21

I'm new to JUnit and Hamcrest and would like best-practice advice so I can decided which documentation to study first.

For starters, which of these assertThat methods is better?

  1. org.junit.Assert.assertThat (from junit-4.11.jar)
  2. org.hamcrest.MatcherAssert.assertThat (from hamcrest-core-1.3.jar)

According to one person last year, "JUnit has the assertThat method, but hamcrest has its own assertThat method that does the same thing.".

According to someone earlier this year, Hamcrest "could potentially give better error messages because the matcher is called to describe the mismatch".

It's hard to tell which versions of Junit and Hamcrest were compared in those posts. So I'd like a recommendation based on the most current released versions.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113

3 Answers3

27

It's almost the exact same thing.

Recent versions of JUnit now include hamcrest.

In fact, org.junit.Assert.assertThat 's method signature is

public static <T> void assertThat(T actual,
                              org.hamcrest.Matcher<T> matcher)

which you will notice uses hamcrest matchers.

You may still want to include your own version of hamcrest because JUnit isn't updated very often and may not always use the latest version of hamcrest.

According to the maven pom, JUnit 4.11 uses hamcrest 1.3 which I believe is the most current as of this writing.

EDIT I've just read your second article http://blog.code-cop.org/2014/02/assert-or-matcherassert.html and it describes 2 slight differences in the hamcrest assertThat that make it more useful:

  1. when the match fails, the error message includes what was different instead of "expected X but was Y". custom hamcrest matchers may include more detailed information about what exactly was wrong by implementing describeMismatch().
  2. The assertThat signature is different in hamcrest using T actual, Matcher<? super T> matcher which allows matchers to be super types (like Matcher to compare Integers and Doubles). This usually doesn't matter but when you need it this is a nice feature to have.

So use org.hamcrest.MatcherAssert.assertThat.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • 1
    Thank you dkatzel, it was helpful to know that JUnit isn't updated very often so I might want to include my own version of Hamcrest. It's that sort of knowledge I was looking for, so thanks again. – Michael Osofsky Dec 02 '14 at 18:40
  • @MichaelOsofsky I've updated my answer which I found that they are slightly different, I now prefer hamcrest. – dkatzel Dec 02 '14 at 18:47
  • Thanks @dkatzel. Do you know if it's possible to control how Eclipse's content-assist feature resolves which assertThat? I'm using Maven with Eclipse. The Eclipse content-assist feature only tells me about Junit or Hamcrest but not both even though I've imported both `import static org.hamcrest.MatcherAssert.*` and `import static org.junit.Assert.*`. – Michael Osofsky Dec 02 '14 at 19:08
  • sorry, I don't know. My guess is eclipse is getting confused since both packages have an `assertThat`. I wouldn't rely on bulk static importing using "*". Instead just `import static org.hamcrest.MatcherAssert.assertThat;` – dkatzel Dec 02 '14 at 19:17
  • Thanks @dkatzel, I think that's the way to go. – Michael Osofsky Dec 02 '14 at 19:53
  • In `JUnit` **4.11** it already uses `Matcher super T> matcher` version (http://junit.org/junit4/javadoc/4.11/org/junit/Assert.html#assertThat(T,%20org.hamcrest.Matcher)). The `Matcher matcher)` version is used in `JUnit` **4.10** (http://junit.org/junit4/javadoc/4.10/org/junit/Assert.html#assertThat(T,%20org.hamcrest.Matcher)) – Manuel Jordan Nov 01 '16 at 13:55
6

An excerpt from JUnit 5's User Guide:

However, JUnit Jupiter’s org.junit.jupiter.Assertions class does not provide an assertThat() method like the one found in JUnit 4’s org.junit.Assert class which accepts a Hamcrest Matcher. Instead, developers are encouraged to use the built-in support for matchers provided by third-party assertion libraries.

I think for JUnit 4, Hamcrest's assertThat is (officially) preferred.

Jeremy Kao
  • 853
  • 10
  • 14
2

In junit 4.13 the method org.junit.Assert.assertThat() is marked deprecated and the usage of org.hamcrest.MatcherAssert.assertThat() is recommended.

So better use hamcrest right away even when you use an older version of junit.

Datz
  • 3,156
  • 3
  • 22
  • 50