Is org.junit.Assert.assertThat better than org.hamcrest.MatcherAssert.assertThat?

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.


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.


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.