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?
If you're using JUnit's org.junit.Assert#assertThat
then upgrade to JUnit 4.11 to pick up a bug fix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With