I changed some method somewhere in our code which shouldn't have caused any weird test failures, but JMock seems to think otherwise.
I boiled the issue down to the minimal amount of cruft, which looks something like this:
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;
public class TestMocking {
@Test
public void test() {
Mockery mockery = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
final Owner owner = mockery.mock(Owner.class);
final RealThing thing = mockery.mock(RealThing.class, "thing");
mockery.checking(new Expectations() {{
oneOf(owner).getThing(); will(returnValue(thing));
oneOf(thing).method(); will(returnValue(Collections.emptyList()));
}});
owner.getThing().method();
mockery.assertIsSatisfied();
}
public static interface Owner {
BaseThing getThing();
}
public static interface BaseThing {
Collection<?> method();
}
public static interface RealThing extends BaseThing {
List<?> method();
}
}
(Edit: This now uses a ClassImposteriser even though there are no classes anymore, because I wanted to demonstrate that you could run the exact same code without that imposteriser and the test would pass.)
The result of running this:
unexpected invocation: thing.method()
expectations:
expected once, already invoked 1 time: owner.getThing(); returns <thing>
expected once, never invoked: thing.method(); returns <[]>
what happened before this:
owner.getThing()
So there you go, "unexpected" thing.method() when the expected thing.method() was never called. I have previously seen this occur when multi-threaded classes are under test against mock objects, but this time it's all happening in a single thread. It's like JMock is somehow returning a different mock object from the first method call, even though I mocked no such object.
If I remove the overridden method which is returning a more specific type then it goes away, but I obviously can't do that. Likewise, if I remove the use of ClassImposteriser, the problem goes away, but one of the objects I'm mocking in the real test is someone else's class. I guess I could try using two Mockery instances in the one test, but aside from that I'm out of ideas.
Hiding class (static) methods doesn't work quite the same as Overriding instance methods. To prove that JMock is not to blame here, try this:
public class test3 {
public static void main(String[] args) {
Owner owner = new Owner();
owner.getThing().method(); //Like how you execute your test
RealThing thing = new RealThing();
thing.method(); //Similar to your mock.
}
private static class Owner {
private BaseThing thing = new RealThing();
public BaseThing getThing() {
return thing;
}
}
private static class BaseThing {
public static void method() {
System.out.println("Basething!");
}
}
private static class RealThing extends BaseThing {
public static void method() {
System.out.println("Realthing!");
}
}
}
Note that the two calls to method()
print different things! Both are instances of RealThing, but they call different methods. The static method called depends on whether it is called from the subcalss or the superclass. In the first call above, method is declared as a BaseClass, so BaseClass.method()
is called, even though it is an instance of RealClass. The second call to method()
is declared as a RealClass, so RealClass.method()
is invoked.
So, the results from JMock are valid. The method()
called was not the same as the one you set up an expectation for.
Don't feel great about my explanation of this. Please do read up on it here: http://docs.oracle.com/javase/tutorial/java/IandI/override.html
The fix (favoring BaseThing.method()
), change:
final RealThing thing = mockery.mock(RealThing.class, "thing");
To:
final BaseThing thing = mockery.mock(RealThing.class, "thing");
Or if you prefer to use RealThing.method()
, change:
owner.getThing().method()
To:
RealThing thing = (RealThing)owner.getThing();
thing.method();