【发布时间】:2013-12-13 12:34:14
【问题描述】:
我正在使用 hamcrest 1.3。它对matches(Object o, Description mismatch) 的实现如下所示:
@Override
public boolean matches(Object o, Description mismatch) {
for (Matcher<? super T> matcher : matchers) {
if (!matcher.matches(o)) {
mismatch.appendDescriptionOf(matcher).appendText(" ");
matcher.describeMismatch(o, mismatch);
return false;
}
}
return true;
}
当描述不匹配时,它首先附加失败的匹配器的描述,然后才是实际的不匹配。这会导致一些非常奇怪的消息。
例如,如果我写:
Description description = new StringDescription();
allOf(lessThan(2)).describeMismatch(5, description);
System.out.println(description.toString());
然后a value less than <2> <5> was greater than <2> 打印在控制台上。我只希望<5> was greater than <2>,因为这是其他匹配器返回的那种消息,例如lessThan 单独使用。
我的问题是:这是一个错误还是我遗漏了什么?
不同的匹配器以不同的格式返回消息这一事实很重要,因为我试图编写一些使用匹配器的自定义验证框架,并且能够在出现任何问题时返回良好的人类可读消息。看来我不能为此目的使用describeMismatch 方法...
【问题讨论】:
标签: java junit matcher hamcrest