对于参数类
class Criteria {
private Map params;
public getMap(){ return params; }
}
和一个服务方法接受这个标准
class Service{
public List<Person> query(Criteria criteria){ ... }
}
自定义 featureMatcher 用于匹配条件键
private Matcher<Criteria> hasCriteria(final String key, final Matcher<?> valueMatcher){
return new FeatureMatcher<Criteria, Object>((Matcher<? super Object>)valueMatcher, key, key){
@Override protected Object featureValueOf(Criteria actual){
return actual.getMap().get(key);
}
}
}
使用 mockito 验证参数时:
verify(Service).query((Criteria) argThat("id", hasCriteria("id", equalTo(new Long(12)))));
错误消息显示:
Argument(s) are different! Wanted:
Service.query(
id <12L>
);
-> at app.TestTarget.test_id (TestTarget.java:134)
Actual invocation has different arguments:
Service.query(
app.Criteria@509f5011
);
如果我使用ArugmentCaptor
,
ArgumentCaptor<Criteria> argument = ArgumentCaptor.forClass(Criteria.class);
verify(Service).query(argument.capture());
assertThat(argument.getValue(), hasCriteria("id", equalTo(new Long(12))));
消息要好得多:
Expected: id <12L> but id was <2L>
我怎样才能得到这样的消息,而不使用ArgumentCaptor
?