有没有办法在Mockito的验证方法中使用类似jUnit Assert消息参数的东西?
让我们假设一段测试代码:
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 将引发异常。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());