Export to GitHub

hamcrest - issue #212

array mismatch description should not just use array element toString value in mismatch description


Posted on Mar 24, 2015 by Grumpy Lion

Using the 'Matchers.array' method to construct a matcher for an array like this:

assertThat(someArrayOfObjects, array(
   myMatcher(...)
));

The descriptions of the failure to match leaves a lot to be desired because it just dumps the 'toString' value of the mismatching array element into the description. So you get something like this:

Expected: [myMatcher(...)] but: element 0 was SomeClass@10efe4ca

The description of what is expected [myMatcher(...)] is clear, but the description of what was actually found is very obscure since its impossible to tell from an object's toString value what is actually 'inside' the object being matched against.

This happens even though I implemented a method to specifically 'describeMismatch' for 'myMatcher' to craft a nice and specific description of the mismatch, but the 'nice' description is not getting used.

This a nuisance at best, but it is very annoying when the objects being matched against do not have nice 'toString'. You could say that maybe one should always have a nice toString, I tend to agree, but sometimes one simply has no control over an object's toString implementation as the objects come from library code.

Status: New