让我们假设一段测试代码:
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
如果模型中的方法没有调用方法,则方法抛出异常Observable.setChanged()
。
问题:如果不添加loggers/System.print.out
,我无法意识到测试失败的当前方法是什么。有没有一种类似于方法的jUnit Assert
方法:
Assert.assertEquals(
String.format("instances %s, %s should be equal", inst1, inst2),
inst1.getParam(),
inst2.getParam()
);
解决方案:
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());