pkdevboxy / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Mocking with reflection #103

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
How about mocking (doReturn-when) with reflection? i.e. method name and
arguments are provided at the run-time. One use that I can find is when
using dynamically generated classes.

class A {
   public B hello(String s) {
      B b;
     //do something
      return b;
   }
}
Object o = new A();
I wish if I can test it doAnswer( new Answer<Object>() { //do something} 
).when(o).invoke("hello").with(Object ... parameters);

when() takes a generic Object and hence wouldn't know what methods are
available.

I understand I lose compile-time safety, but am I asking something
unnecessary? I have a case where my classes are generated on the fly and I
wish to mock their behavior.

Original issue reported on code.google.com by surender...@gmail.com on 30 Jun 2009 at 9:29

GoogleCodeExporter commented 8 years ago
Oops, I must have forgotten about this issue. Let me see...

> when(o).invoke("hello")

when() returns the type of mocked object so 'invoke' will not be available 
there.

>I understand I lose compile-time safety, but am I asking something
>unnecessary? I have a case where my classes are generated on the fly and I
>wish to mock their behavior.

Fair enough. compile-time safety is certainly not a goal - rather nice side 
effect of
the current api. As far as your suggestion is concerned: You're very welcome to
propose an api and contribute :)

Changing it into an enhancement

Original comment by szcze...@gmail.com on 9 Jul 2009 at 12:43

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 9 Jul 2009 at 12:47

GoogleCodeExporter commented 8 years ago
>>when() returns the type of mocked object so 'invoke' will not be available 
there.
correct, my mistake..intended something like whenInvoked(mock, 
"hello").with(..)..
but you got the point anyways...

>>Fair enough. compile-time safety is certainly not a goal - rather nice side 
effect of
>>the current api. As far as your suggestion is concerned: You're very welcome 
to
propose an api and contribute :)

>>Changing it into an enhancement
Great. thanks!

Original comment by surender...@gmail.com on 9 Jul 2009 at 4:28

GoogleCodeExporter commented 8 years ago
After almost three years situation is the same... I`m really missing this 
enhancement now and I think I`ll have to plug PowerMockito only for this 
feature. I saw similar behavior there, but not sure it`ll work, it`s designed 
for testing private methods:
verifyPrivate(tested).invoke("privateMethodName", argument1);

Original comment by anatoliy...@gmail.com on 19 Apr 2012 at 7:37

GoogleCodeExporter commented 8 years ago
I actually do this already by combining Mockito and FEST-Reflect:

  final T spy = spy(injector.getInstance(resourceClass));
  Object ongoingMock = doReturn(response).when(spy);
  method("getResponse").withReturnType(Response.class).in(ongoingMock).invoke();

This actually works really well, I use it for mocking out some methods in the 
superclass of my spied class.

Original comment by m...@talios.com on 19 Apr 2012 at 7:49

GoogleCodeExporter commented 8 years ago
@Anatoliy There was other priorities, it's open source so if you feel you can 
contribute a nice Mocking with reflection API we will welcome your pull request.

Original comment by brice.du...@gmail.com on 21 Apr 2012 at 4:19

GoogleCodeExporter commented 8 years ago
We could potentially provide reflect() method (or some name like that) so that 
one  can do:

when(reflect(mock).call("someMetgod", args)).then ...
doReturn(x).when(reflect(mock)).call("someMethod", args);

Mockito is open source so you know what to do when you need some feature ;)

Original comment by szcze...@gmail.com on 21 Apr 2012 at 9:39