Partial mocking

432 views
Skip to first unread message

obarbeau

unread,
Sep 26, 2008, 9:36:07 AM9/26/08
to mockito
Hi,

I am a little skeptic as for the "spying feature" of Mockito on
objects.
When I "stub" a method M1 of a class which is "spyed", I would expect
that it is always this "stubbed" method which is called and not the
real one.
However, when the call to M1 is done by a method M2 of this same spyed-
class, it is the real method M1 which is called and not the "stubbed"
one !

There is a very simple example below to make my problem clear.
I am used to working with EasyMock, so I also provide the sample with
this framework.

Please give me your opinion on this.
Best Regards,
Olivier Barbeau



public class MyClassToBePartiallyStubbed {

public int getNumber() {
return 3;
}

public void doStuff() {
System.out.println("entering doStuff");
System.out.println("stubbed? = " + getNumber());
System.out.println("leaving doStuff");
}
}

import ...
public class PartialStubTest {

@Test
public void testWithMockito() throws Exception {
MyClassToBePartiallyStubbed spy = spy(new
MyClassToBePartiallyStubbed());
// only stub the getNumber() method
doReturn(5).when(spy).getNumber();

System.out.println("stubbed ok = " + spy.getNumber());
spy.doStuff();
System.out.println("stubbed ok = " + spy.getNumber());
}

@Test
public void testWithEasyMock() throws Exception {
// only stub the getNumber() method
MyClassToBePartiallyStubbed partiallyMocked =
EasyMock.createMock(MyClassToBePartiallyStubbed.class,
MyClassToBePartiallyStubbed.class.getMethod("getNumber",
(Class<?>[]) null));

EasyMock.expect(partiallyMocked.getNumber()).andReturn(5).anyTimes();
EasyMock.replay(partiallyMocked);

System.out.println("stubbed ok = " + partiallyMocked.getNumber());
partiallyMocked.doStuff();
System.out.println("stubbed ok = " + partiallyMocked.getNumber());
}
}

Here's the results:
1/ With Mockito:
stubbed ok = 5
entering doStuff
stubbed? = 3 <-- why ?!?
leaving doStuff
stubbed ok = 5

2/ With EasyMock
stubbed ok = 5
entering doStuff
stubbed? = 5 <-- OK
leaving doStuff
stubbed ok = 5

szczepiq

unread,
Sep 26, 2008, 4:33:36 PM9/26/08
to moc...@googlegroups.com
Hi,

You're exactly right, spying on real objects in Mockito is not like
partial mocking in EasyMock. I cannot say we did it on purpose - the
implementation was the simplest possible. Plus, we cared about the
interface - to keep it clean enough, to keep constructors
refactorable, etc. Also, the case we implemented this feature for was
a bit different. Look, mockito spies were meant to help testing other
classes - not the spy itself (in your case:
MyClassToBePartiallyStubbed). This is where we can start a debate on
partial mocking: pure evil or handy tool :) (Please not unless you
really want to).

I agree that according to the javadoc you expect the mockito spy deal
with the case you provided. I will fix the docs. I don't know a clean
way of implementing EM's style of partial mocking - do you?

Cheers,
Szczepan Faber

obarbeau

unread,
Oct 9, 2008, 8:36:35 AM10/9/08
to mockito
Thanks for your quick answer Szczepan. I understand your point of view
and I beleive that the docs should be updated.
I don't have a simple answer to this case so I'll refactor my tests to
avoid this kind of evil-mocking...

long life to Mockito :)

szczepiq

unread,
Oct 9, 2008, 8:56:53 AM10/9/08
to moc...@googlegroups.com
hehe, thanks!
Reply all
Reply to author
Forward
0 new messages