Strange AllOf hamcrest matcher mismatch description

为君一笑 提交于 2019-12-10 17:44:13

问题


I'm using hamcrest 1.3. Its implementation of matches(Object o, Description mismatch) looks like that:

@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;
}

When describing mismatch, it first appends the description of matcher that failed, and only then the actual mismatch. This results in some pretty odd message.

For example, if I write:

Description description = new StringDescription();
allOf(lessThan(2)).describeMismatch(5, description);
System.out.println(description.toString());

Then a value less than <2> <5> was greater than <2> is printed the console. I would expect just the <5> was greater than <2>, because that's the kind of message the other matchers return, e.g. lessThan used alone.

My question being: is this a bug or am I missing something?

The fact that different matchers return messages in different formats is a big deal, since I was trying to write some custom validation framework that uses the matchers and is able to return nice human readable messages if anything goes wrong. It seems that I can't use the describeMismatch method for that purpose...


回答1:


I'd say it's a (usability) bug. The message could be more expressive simply with the word "but" instead of the space.

It's worth pointing out that the mismatch messages are normally displayed by JUnit's assertThat() with some additional words: "Expected" (shows the Matcher description), "Actual" (shows the mismatch). This makes it clearer.

Normally when you use an allOf() you'd have more than one matcher, so it's useful for the mismatch description to say which one broke. But I agree the message could be clearer.



来源:https://stackoverflow.com/questions/20566668/strange-allof-hamcrest-matcher-mismatch-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!