Is there any way in JMock2 to let a test pass when reaching the execution of a given mock's method call? In other words, I would like to write some test code like:
assertTrue(when(aRequestMock).methodCall());
To test production code like:
public void methodUnderTest(){
// Some initialization code and members call
request.foo();
String a = anotherInstance.bar();
// many more calls to follow
}
...so I would not need to mock 'anotherInstance.bar()'
return value as well as any other mock call to follow?
I know that it would not represent any strict check and it cannot be considered a best practice, yet it would come handy when testing methods with a long list of members' methods.
Given code:
public void methodUnderTest(){
request.foo();
anInstance.bar();
yetAnotherInstance.baz();
}
you cannot skip the execution of bar()
and baz()
once foo()
is invoked. Let me also say that you don't want to do that, because even if you could skip it in the test, it's going to be executed in production anyway, so you'd better test it, too :-)
The nearest thing you can do is
context.checking(new Expectations() {{
oneOf(requestMock).foo();
ignoring(anInstanceMock).bar();
ignoring(yetAnotherInstanceMock).baz();
}});
Here I'm using oneOf()
on the first line because foo()
is the focus of your test. You can also simplify this by not mentioning bar()
and baz()
:
context.checking(new Expectations() {{
oneOf(requestMock).foo();
ignoring(anInstanceMock);
ignoring(yetAnotherInstanceMock);
}});
However, keep in mind that by doing so you will ignore any method invocation on anInstanceMock
and yetAnotherInstanceMock
.