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?
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.
assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert. assertThat defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.
assertEquals() is the method of Assert class in JUnit, assertThat() belongs to Matchers class of Hamcrest. Both methods assert the same thing; however, hamcrest matcher is more human-readable. As you see, it is like an English sentence “Assert that actual is equal to the expected value”.
Other test frameworks For example, Hamcrest can be used with JUnit (all versions) and TestNG.
Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit. You can also define custom matcher implementations.
If you have used Hamcrest with JUnit 4, you will probably remember that you had to use the assertThat () method of the org.junit.Assert class. However, the JUnit 5 API doesn't have a method that takes a Hamcrest matcher as a method parameter. The JUnit 5 User Guide explains this design decision as follows:
The JUnit 5 User Guide explains this design decision as follows: However, JUnit Jupiter's org.junit.jupiter.api.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.
The JUnit Assert.assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert.assertThat method defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.
Let's move on and find out how we can write assertions for lists. If we want to write an assertion which verifies that the size of a list is correct, we have to create our Hamcrest matcher by invoking the hasSize () method of the Matchers class. For example, if we want to verify that the size of a list is 2, we have to use this assertion:
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:
describeMismatch()
.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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With