JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

AllOf matcher adds misleading describeTo to mismatch description

Open shalak opened this issue 5 years ago • 0 comments

Hi!

Let's say I have a custom isOpened() matcher that checks if a page has been opened. It has the following methods:


    @Override
    public void describeTo(Description description) {
        description.appendText("Page was opened");
    }

    @Override
    protected void describeMismatchSafely(T assertedPage, Description mismatchDescription) {
        mismatchDescription
            .appendText("Wrong url opened:" + getCurrentUrl());
    }

When I'm using this matcher standalone and it asserts, here's what I see:

Expected: Page was opened
     but: Wrong url opened: http://wrong.example.com

This is great and informative. Unfortunately, when I use my matcher as part of allOf(), here's what I see:

Expected: (Page is opened and <another_matcher's_describe_to>)
     but: Page is opened Wrong URL opened: http://wrong.example.com

Notice the "Page is opened" within the "but" section. The AllOf matcher adds the matcher description to the mismatch description. This is the matches method of AllOf matcher:

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

It's because of mismatch.appendDescriptionOf(matcher).appendText(" ");. Why is it there? Am I missing something and I misinterpreted the API? I assumed that the describeTo is responsible for the Expected: part, and the describeMismatch (or describeMismatchSafely in my case) is for the but: part...

shalak avatar Mar 19 '20 10:03 shalak