Override Mockito toString test output
hamcrest, java, junit, mockito
Solution
So I was doing something silly; I was reading the Javadoc for Matcher when I really should have been looking at the Javadoc for ArgumentMatcher.
Once I realized my mistake, it's easy; just override the `describeTo` method defined in that interface, e.g.
@Override
public void describeTo(Description description) {
description.appendText(String.valueOf(expected));
description.appendText(" ± ");
description.appendText(String.valueOf(delta));
}
Problem
I have written a custom Hamcrest `Matcher<Double>` to use with `Mockito.doubleThat`. I want to "override the `toString()`" method, so to speak, so that if there's a failure, the error is more verbose. Here's the JUnit failure trace: ``` Argument(s) are different! Wanted: dependedOnComponent.method( <Double matcher> ); -> at my.domain.TestClass.testMethod(TestClass.java:123) Actual invocation has different arguments: dependedOnComponent.method( 123.45, ); -> at my.domain.SystemUnderTest.callingMethod(SystemUnderTest.java:456) ``` As you can see, it prints `<Double matcher>`. Is it possible to override that message? Instead, I would like to see, as an example: ``` Argument(s) are different! Wanted: dependedOnComponent.method( 120 < matcher < 121 ); ``` But a different instantiantion of my matcher class might be: ``` Argument(s) are different! Wanted: dependedOnComponent.method( 1 < matcher < 200 ); ``` I don't need to know how to write the code to generate the numbers or the syntax, I just need to know WHERE to put it.