Let's assume a snippet of testing code:
Observable model = Class.forName(fullyQualifiedMethodName).newInstance();
Observer view = Mockito.mock(Observer.class);
model.addObserver(view);
for (Method method : Class.forName(fullyQualifiedMethodName).getDeclaredMethods())
{
method.invoke(model, composeParams(method));
model.notifyObservers();
Mockito.verify(
view, Mockito.atLeastOnce()
).update(Mockito.<Observable>any(), Mockito.<Object>any());
}
Mockito.verify
method throws an exception if a method in a model hasn't invoked Observable.setChanged()
method.
Problem: without adding loggers/System.print.out
I can't realize what's the current method that has failed the test. Is there a way of having something similar to jUnit Assert
methods:
Assert.assertEquals(
String.format("instances %s, %s should be equal", inst1, inst2),
inst1.getParam(),
inst2.getParam()
);
SOLUTION:
verify(observer, new VerificationMode()
{
@Override
public void verify(VerificationData data)
{
assertTrue(
format(
"method %s doesn't call Observable#setChanged() after changing the state of the model",
method.toString()
),
data.getAllInvocations().size() > 0);
}
}).update(Mockito.<Observable>any(), Mockito.<Object>any());
This question is ancient, but Mockito v2.1.0+ now has a built-in feature for this.
verify(mock, description("This will print on failure")).someMethod("some arg");
More examples included from @Lambart's comment below:
verify(mock, times(10).description("This will print if the method isn't called 10 times")).someMethod("some arg");
verify(mock, never().description("This will print if someMethod is ever called")).someMethod("some arg");
verify(mock, atLeastOnce().description("This will print if someMethod is never called with any argument")).someMethod(anyString());