what's the difference between doAnswer & thenAnswer

5,201 views
Skip to first unread message

zero_zone

unread,
Jul 19, 2011, 11:10:57 PM7/19/11
to mockito
Hi,

I feel puzzled with the two functions of doAnswer and thenAnswer?

Are they both the solutions of stubbing with callbacks?

I think below two examples have the same functions.Is it right?

doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Mock mock = invocation.getMock();
return null;
}})
.when(mock).someMethod();


when(mock.someMethod(anyString())).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + args;
}
});


So who can tell me the difference between doAnswer and thenAnswer?
Thank you very much!

Brice Dutheil

unread,
Jul 20, 2011, 4:25:45 AM7/20/11
to moc...@googlegroups.com
Hello,

Yes they both do have the same purpose. It's the writing style that change.

However when you want to stub a method returning void you need to use 'doAnswer'.


-- 
Bryce



--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


zero_zone

unread,
Jul 21, 2011, 1:01:15 AM7/21/11
to mockito
Hi Bryce,

I got it. Thanks a lot!
> >http://groups.google.com/group/mockito?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -
Reply all
Reply to author
Forward
0 new messages