Similar to DescribedAsMatcher, ContextMatcher wraps a matcher with context, displayed as
"<expected> (context)". This is mainly for use in parameterized tests, such as (from ijdk):
@Test
@Parameters
@TestCaseName("{method} {index} {params}")
public <T extends Comparable<T>> void compare(int expected, T x, T y) {
int result = Comp.compare(x, y);
assertThat(result, withContext(equalTo(expected), "x: " + x + "; y: " + y));
}
The optional usage of instances of Message will result in
toString being called only
when necessary, that is, for a failed matcher. Therefore there is less overhead than using simple
strings.