Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a version of JUnit assertThat which uses the Hamcrest 'describeMismatch' functionality?

In every version of JUnit I have tried (up to 4.8.1), a failing assertThat will display an error message that looks like:

expected: [describeTo]
got: [String representation of object]

In other words, it will display the toString() of the object instead of the mismatch description from the Matcher. If I use the assertThat from org.hamcrest.MatcherAssert.assertThat, then it will call 'describeMismatch' and display a more helpful error message.

Am I using Junit incorrectly or is there currently no version of JUnit that will do what I want? Do most people use the Hamcrest assertThat then?

like image 581
Jacob Avatar asked Oct 12 '10 14:10

Jacob


2 Answers

Short answer: no.

As far as I can tell, the most recent version of Hamcrest (1.2) has introduced type signatures which are incompatible with version 1.1, which JUnit currently depends on. I am not sure the extent of the damage (so to speak) created by the change in Hamcrest, but it does not appear that the JUnit team are in any hurry to upgrade (see the open issue).

I am not entirely sure I have solved my issue, but I am planning to use MatcherAssert.assertThat(). This can require a specific release of JUnit (junit-dep-xxx I believe) which will not have classpath conflicts with Hamcrest. Otherwise you may receive NoSuchMethodErrors when assertThat() makes the call to describeMismatch().

like image 108
Grundlefleck Avatar answered Nov 12 '22 22:11

Grundlefleck


Yes, this is fixed by this commit in JUnit 4.11:

As pointed out by Steve Freeman of Hamcrest, the Matcher interface now has an additional method describeMismatch. To be safe to catch such improvements in the future, MatcherAssert is used instead of duplicating its implementation.

Along with the upgrade to Hamcrest 1.3 (also in 4.11), these two versions work well together.

like image 20
Joe Avatar answered Nov 12 '22 22:11

Joe