doNothing() not work or I misunderstood its behavior?

12,732 views
Skip to first unread message

Jarod Liu

unread,
Nov 21, 2008, 7:03:37 AM11/21/08
to mockito
jdk 1.6
junit 4.4
mockito 1.6

public class MyClass {

public void method_to_be_test() {
method_to_be_mock();
}

void method_to_be_mock() {
throw new IllegalStateException("should not go here");
}

}

public class MyClassTests {

@Test
public void testMethod_to_be_test() {
MyClass instance = new MyClass();
MyClass spy = spy(instance);
doNothing().when(spy).method_to_be_mock();
spy.method_to_be_test();
}

}

when run the test:

java.lang.IllegalStateException: should not go here
at demo.MyClass.method_to_be_mock(MyClass.java:10)
at demo.MyClass.method_to_be_test(MyClass.java:6)
at demo.MyClass$$FastClassByCGLIB$$72a36a01.invoke(<generated>)
at org.mockito.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.mockito.internal.MockHandler.intercept(MockHandler.java:87)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept
(MethodInterceptorFilter.java:45)
at demo.MyClass$$EnhancerByCGLIB$$9636116e.method_to_be_test
(<generated>)
at demo.MyClassTests.testMethod_to_be_test(MyClassTests.java:14)


Bartosz Bańkowski

unread,
Nov 21, 2008, 7:10:57 AM11/21/08
to moc...@googlegroups.com
I think we had similar topic recently. Please look at the
documentation (13. Spying on real objects):
http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html

"Mockito spies are not partial mocks. Mockito spy is meant to help
testing other classes - not the spy itself. Therefore spy will not
help if you intend to verify if method calls other method on the same
object."

It should be fine if you try spy.method_to_be_mock(), but
spy.method_to_be_test() will invoke method on the original instance.
That's why you have exception there.

Regards,
Bartosz Bankowski

Jarod Liu

unread,
Nov 22, 2008, 3:08:16 AM11/22/08
to mockito
>> Mockito spy is meant to help testing other classes
I really don't get it. Why I would need a Class2 spy when I testing
Class1? The dependence of Class2 in Class1 should be isolated(using
mock).
I thinking partial mock would be more useful.

On Nov 21, 8:10 pm, "Bartosz Bańkowski" <bbankow...@gmail.com> wrote:
> I think we had similar topic recently. Please look at the
> documentation (13. Spying on real objects):http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mo...
>
> "Mockito spies are not partial mocks. Mockito spy is meant to help
> testing other classes - not the spy itself. Therefore spy will not
> help if you intend to verify if method calls other method on the same
> object."
>
> It should be fine if you try spy.method_to_be_mock(), but
> spy.method_to_be_test() will invoke method on the original instance.
> That's why you have exception there.
>
> Regards,
> Bartosz Bankowski
>

szczepiq

unread,
Nov 23, 2008, 6:49:41 PM11/23/08
to moc...@googlegroups.com
Oh dear, not again partial mocks :)

I don't like partial mocks because they seem to deliver a wrong
message. Traditional mocks say: "hey, this logic belongs to a
different object, go ahead and create new interface/class so you can
mock it". Partial mocks seem to say: "hey, don't worry about SRP
(single responsibility principle) and leave the logic in the same
class. Create a new method so you can partial-mock it".

Also, partial mocks make my test dependent on the *invocations between
internal methods* of the class. This limits refactoring and makes my
test focused on less important implementation details instead of
focused on interesting behavior. I'm fine when the test depends on the
*invocations on dependencies*. Why? Well, because it's a sole of
object orientation to me: objects delegating resposibilities to other
objects.

This is of course my POV, written late night so I'm not 100% sure it
all makes sense... There are probably hundreds of happy
partial-mockers out there so don't worry too much if you disagree with
me. There are also older threads on this list with smarter opinions on
partial mocking.

Spying in mockito is a bit unfortunate feature because it leads to the
questions you raised. Also, I bet there are people cursing mockito
"darn spying doesn't work!" but they are not so OS-enabled to send an
email to our list. I guess mockito might evolve into partial mocking
(with a dear hope that people will use it occasionaly and
judicisiosly).

Cheers,
Szczepan Faber

2008/11/22 Jarod Liu <liuyu...@gmail.com>:

alexand...@gmail.com

unread,
Dec 8, 2008, 7:47:02 PM12/8/08
to mockito

Don't partially mock.
Your tests should go like this:

Step 1: prepare context (setup input parameters for the behaviour to
test)

Step 2: prepare collaborators (objects used by the class under
testing): mocks, stubbs

Step 3: call method to test from object under testing

Steps 4: verify behaviour (the asserts)


At Step 3. the object under testing is real (the only overides are
getter injections); injection can also be done through setters or
constructor (some recomend this, not me :P )

Reply all
Reply to author
Forward
0 new messages