has when() changed to longer call the real method

5,859 views
Skip to first unread message

welk

unread,
Mar 9, 2012, 2:52:18 PM3/9/12
to mockito
There is somebody on stackoverflow asking how to not have their real
method called during stubbing:
http://stackoverflow.com/questions/9638193/how-to-setup-a-call-to-method-of-mocked-object-in-mockito-without-calling-the-or/9639367#9639367
. I know I used to experience the same issue, but one of the
responders says that under 1.9 they are not having that side-effect.
Did this behavior change during one of the last few releases?

Brice Dutheil

unread,
Mar 9, 2012, 4:00:25 PM3/9/12
to moc...@googlegroups.com
The posted code is working in Mockito since ages.

However I suppose the author might want to stub a final method or class, and it is not available in Mockito. The resulting behavior is the real method being called.


Tom's answer is correct.


Hope that helps.


-- Brice




--
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.


welk

unread,
Mar 10, 2012, 6:57:26 PM3/10/12
to mockito
Thanks Brice.

Eric Lefevre-Ardant

unread,
Sep 27, 2012, 4:11:38 AM9/27/12
to moc...@googlegroups.com
Ah, this is a common pitfall when using spies.

You need to understand that you could rewrite this:
  Mockito.when(spy.test("Tom")).thenReturn("not Tom");
as:
  String temp = spy.test("Tom");
  Mockito.when(temp).thenReturn("not Tom");

This would be strictly equivalent code!

Instead, try the following:
  Mockito.doReturn("not Tom").when(spy).test("Tom");

This allows Mockito to intercept the call to test() *before* it happens.

On 27 September 2012 05:08, 徐兵 <vividb...@gmail.com> wrote:
hi Brice, i hava a similar case in my project , except that i use the "spy" method to replace the "mock" method . Actually , the real method i want to spy executed indeed .
And here is my code.
SpyTest.class 
     public class SpyTest {

public String test(String name){
System.out.println("only show when it doesn't beening mocked");
return name;
}
     }

testcase 
         @Test
public void testSpy(){
SpyTest spyTest = new SpyTest();
//mock
                SpyTest mock = Mockito.mock(spyTest.getClass());
Mockito.when(mock.test("Tom")).thenReturn("not Tom");
System.out.println(mock.test("Tom"));
//spy
SpyTest spy = Mockito.spy(spyTest);
Mockito.when(spy.test("Tom")).thenReturn("not Tom");
System.out.println(spy.test("Tom"));
}

here is the result 

not Tom
only show when it doesn't beening mocked
not Tom

Is there any way could i make the real method I spied not execute?


在 2012年3月10日星期六UTC+8上午5时00分25秒,Brice写道:
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/tEoeyxFluIYJ.

Brice Dutheil

unread,
Sep 27, 2012, 7:07:01 AM9/27/12
to moc...@googlegroups.com
Yes,

Spies default behavior is to execute the real code. And as Eric highlighted spy.test(...) is executed before the stub when(...).then(...)
In some case switching to the alternate syntax that Eric mentionned might be necessary.


Please note as well that you should avoid partial mocks as much as possible in your code, using partial mocks reveal flawed object oriented design. I.e. you spied object has too much responsibilities.

Cheers,
-- Brice

徐兵

unread,
Sep 28, 2012, 7:35:55 AM9/28/12
to moc...@googlegroups.com
Brice and  Eric,thanks!
But in my opinion,using the same when(...)then(...) method,it will be better if spy can avoid calling the real method when i don't need .

2012/9/27 Brice Dutheil <brice....@gmail.com>

Eric Lefevre-Ardant

unread,
Sep 28, 2012, 7:37:19 AM9/28/12
to moc...@googlegroups.com
Well, this sounds like a technical issue related to the Java language. Good luck fixing that...
Reply all
Reply to author
Forward
0 new messages