PasqualeJ / mockito

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

Specify custom default Answer in @Mock #345

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The @Mock annotation currently only supports default answers from the Answers 
enum. Eg:

@Mock(answer=Answers.RETURNS_SMART_NULLS)
private Foo mockFoo;

I would like to be able to specify my own default Answer implementation. Eg:

@Mock(customAnswer=MyCustomAnswer.class)
private Foo mockFoo;

Original issue reported on code.google.com by ukla...@gmail.com on 28 May 2012 at 8:37

GoogleCodeExporter commented 9 years ago
Hi,

Thanks for the idea.

This feature is interesting, it was discussed on the mailing-list some time ago 
already. At that time it was brought in another topic regarding an interesting 
mock framwork based on top of mockito : http://code.google.com/p/komarro/

The feature is interesting but it raises some questions regarding the state / 
initialization of the answer if any.

Anyway if you want to propose some code, that would be great, even if it's 
prototype.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 29 May 2012 at 6:43

GoogleCodeExporter commented 9 years ago
If issue 346 (Answers.THROWS_EXCEPTION) was implemented, I would not need to 
resolve this issue (345).

I was thinking that Mockito could just use the zero args constructor to 
instantiate the custom answer and anything more complex would have to be done 
manually (without annotations).

Please let me know if you'd consider resolving issue 346. If not, I might have 
to get my hands dirty to provide a patch/prototpye.

Original comment by ukla...@gmail.com on 29 May 2012 at 7:53

GoogleCodeExporter commented 9 years ago
Of course for simple answers that would be it, that's very easy to add in the 
code base actually.

Though, it should be interesting to anticipate complex answer, instead of 
cluttering the API for an incomplete feature, that could be deprecated.

Also I don't think the team agrees with issue 346, at least with the proposed 
usage.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 29 May 2012 at 8:17

GoogleCodeExporter commented 9 years ago
I think this would just be moving a configuration available when using mock() 
method, as specified in documentation:
>>14. Changing default return values of unstubbed invocations (Since 1.7) 
>>Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); 

This thing is not available in annotation as it expectes an Enum Answers 
insteads of something implementing Answer interface

Original comment by tomasz.b...@pragmatists.pl on 20 Jan 2014 at 10:39

GoogleCodeExporter commented 9 years ago
Ir is not possible to provide in an annotation a live type, or event an enum 
implementing an interface.

An annotation attribute accepts only the following constants :

* primitive
* String
* Class
* Enum
* Annotation
* array of any of the above

see JLS §9.7 ⇒ 
http://docs.oracle.com/javase/specs/jls/se5.0/html/interfaces.html#9.7

Original comment by brice.du...@gmail.com on 22 Jan 2014 at 5:43

GoogleCodeExporter commented 9 years ago
Ok, still it is possible to specify class, which could be instantiated by 
Mockito runner and used as default answer

Original comment by kretesen...@gmail.com on 22 Jan 2014 at 9:44

GoogleCodeExporter commented 9 years ago
Yes that is an option, we talked about it some time ago on the mailing list. Do 
you want to make a PR on github ?

Original comment by brice.du...@gmail.com on 23 Jan 2014 at 12:04

GoogleCodeExporter commented 9 years ago
I'm ok with this feature but I probably won't be working on this since I 
consider it low prio.

Original comment by szcze...@gmail.com on 27 Jan 2014 at 7:02

GoogleCodeExporter commented 9 years ago
We have submitted a PR https://github.com/mockito/mockito/pull/34

Original comment by tomasz.b...@pragmatists.pl on 29 Jan 2014 at 8:43

GoogleCodeExporter commented 9 years ago
I would love to have this extension of the Mock annotation.
For example I need it to mock GWT Messages interfaces with an implementation 
that uses MessageFormat to fake the GWT.create of such objects in unit tests.

I also subclass the ReturnsDeepStubs to mock my View interfaces. These 
interfaces need to implement IsWidget which returns a Widget (which tries to 
call into GWT.create).

Right now I need to use the mock( MyMessages.class, new GWTMessagesAnswer()); 
to init the mocks, it would be much easier if I could use the annotation 
instead.

@Mock(answer=GWTMessagesAnswer.class)
MyMessages messages;

Original comment by david.nouls on 6 May 2014 at 9:21