Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest describeMismatchSafely always printing Object.toString() insted of my implementation

I wrote a custom matcher to compare my objects. It all works except for the describeMismatchSafely method. I kept simplyfing and simplyfing, until I got this:

public static TypeSafeMatcher<IMyObj > equalTo(final IMyObj expected) {

    return new TypeSafeMatcher<IMyObj >() {

        @Override
        public void describeTo(final Description description) {
            description.appendText("this value");
        }

        @Override
        public void describeMismatchSafely(final IMyObj myObj, final Description mismatchDescription) {
            mismatchDescription.appendText(" the wrong value");
        }

        @Override
        protected boolean matchesSafely(IMyObj actual) {
            return false;
        }
   }
}

The method describeTo works just fine, but describeMismatchSafely always prints myObj.toString() instead of the message I want it to:

java.lang.AssertionError: Expected: this value got:

I googled custom matchers implementations and it seemed everybody was overriding the describeMismatchSafely method and it was working just fine for them. Is there a reason mine should not work?

like image 891
Mauzik Avatar asked Dec 23 '14 09:12

Mauzik


1 Answers

If you're using JUnit's org.junit.Assert#assertThat then upgrade to JUnit 4.11 to pick up a bug fix.

like image 191
Joe Avatar answered Nov 10 '22 16:11

Joe