How to get original class of a mocked object for reflection purposes?

2,330 views
Skip to first unread message

KARR, DAVID

unread,
May 8, 2014, 6:37:54 PM5/8/14
to moc...@googlegroups.com
I have one utility class that takes a class and method name and a list of parameters, and does some reflection to call the method. This works fine if no mock objects are involved. If any of the parameters are mock objects, the "getDeclaredMethod()" call fails to return the method I need. I implemented a workaround of allowing an optional array of parameter types to override the direct types of the parameters. However, it occurred to me that perhaps Mockito already provides a way for me to detect that a particular object is a mock object, and if so, to get the original class of the object. Is that possible?
winmail.dat

Szczepan Faber

unread,
May 9, 2014, 5:43:07 AM5/9/14
to moc...@googlegroups.com
>However, it occurred to me that perhaps Mockito already provides a way for me to detect that a particular object is a mock object, and if so, to get the original class of the object. Is that possible?

There is no public API that could satisfy this. It could be added as a
new method to MockingDetails.

Cheers!
--
Szczepan Faber
Principal engineer@gradle; Founder@mockito
Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA:
http://www.gradlesummit.com

KARR, DAVID

unread,
May 9, 2014, 5:26:04 PM5/9/14
to moc...@googlegroups.com
> -----Original Message-----
> From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On Behalf
> Of Szczepan Faber
> Sent: Friday, May 09, 2014 2:43 AM
> To: moc...@googlegroups.com
> Subject: Re: [mockito] How to get original class of a mocked object for
> reflection purposes?
>
> >However, it occurred to me that perhaps Mockito already provides a way
> for me to detect that a particular object is a mock object, and if so, to
> get the original class of the object. Is that possible?
>
> There is no public API that could satisfy this. It could be added as a
> new method to MockingDetails.

Ok. I've filed an issue at least. This sounds like something I should be able to do myself. I'll report back on the ticket.

Szczepan Faber

unread,
May 9, 2014, 5:30:37 PM5/9/14
to moc...@googlegroups.com
Excellent, feel free to contribute :)
> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mockito.
> For more options, visit https://groups.google.com/d/optout.

KARR, DAVID

unread,
May 10, 2014, 1:02:02 PM5/10/14
to moc...@googlegroups.com
Ok, now I'm confused. I think we have a disconnect here. I finally got into the code and looked at the "MockingDetails" interface. I've never seen this class before, but I first noticed the single statement in the class javadoc: "you can identify whether a particular object is either a mock or a spy". I thought this was curious, since this was exactly what I was trying to determine. I then saw the example of how to call "mockingDetails(obj).isMock()" and I made some simple calls to that in my test, passing both mock objects and non-mock objects, and it does exactly what I expect, which is exactly what I was looking for.

So what did you think I was asking for, or did you simply forget that method and implementation was there?

Szczepan Faber

unread,
May 10, 2014, 4:37:06 PM5/10/14
to moc...@googlegroups.com
I thought that you wanted to know the _class_ of the mock. Eg.

List list = mock(List.class);
assert mockingDetails(list).getMockedClass() == List.class

I made up 'getMockedClass' - such method does not exist and I'm not
sure I like this name :)

If you're happy with the current API, don't add the new method and stay happy :)

Cheers!

KARR, DAVID

unread,
May 10, 2014, 8:55:17 PM5/10/14
to moc...@googlegroups.com
> -----Original Message-----
> From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On Behalf
> Of Szczepan Faber
> Sent: Saturday, May 10, 2014 1:37 PM
> To: moc...@googlegroups.com
> Subject: Re: [mockito] How to get original class of a mocked object for
> reflection purposes?
>
> I thought that you wanted to know the _class_ of the mock. Eg.
>
> List list = mock(List.class);
> assert mockingDetails(list).getMockedClass() == List.class
>
> I made up 'getMockedClass' - such method does not exist and I'm not
> sure I like this name :)

I've also been struggling with what it would be called.

> If you're happy with the current API, don't add the new method and stay
> happy :)

Under what circumstances would the "mockedClass" not be the superclass, if it's a mock or spy?

Brice Dutheil

unread,
Jun 27, 2014, 12:37:12 PM6/27/14
to moc...@googlegroups.com
When you mock an interface ;)

Actually I would probably look at the mocked type somewhere in the mock settings.

-- Brice

Michael Vorburger

unread,
Jul 26, 2016, 2:58:06 AM7/26/16
to mockito
Just found this post through Google. FTR & for future searchers:

It looks like MockUtil (now?) has a getMockName(), and may be what you're looking for here.

Brice Dutheil

unread,
Jul 26, 2016, 4:45:43 AM7/26/16
to moc...@googlegroups.com
Since this thread is woken up I suggest now to use `Mockito.mockingDetails(the_mock).getMockType()` it's available since quite some time in the 2.0.x betas.

-- Brice

Reply all
Reply to author
Forward
0 new messages