John Helper
John Helper

Reputation: 29

Using assertThat in JUnit4

This link says the function assertThat() is in both JUnit4 and JUnit5.

https://www.baeldung.com/junit-assertions

However, according to the source code of JUnit4 from the creator team, assertThat() is not included: http://s.bl-1.com/h/cs6JWS1r?url=https://github.com/junit-team/junit4/tree/master/src/main/java/junit/framework

Having not used Maven or JUnit too much, how do these reconcile? I have starter code that uses JUnit4 and Assert.assertThat, but as is sensible from the source code, assertThat() does not exist as a function. An error confirms this. This code is supposed to work out of the box.

Upvotes: 0

Views: 2667

Answers (1)

jbx
jbx

Reputation: 22178

The code for assertThat is included here: https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java (the package is different than the one you were looking at, so probably you just have the wrong import.)

However you will probably need to use the Hamcrest library along with JUnit for assertThat() to work, since you need appropriate Matchers.

On the same page you posted there is a link to here: https://www.baeldung.com/java-junit-hamcrest-guide

A bit more details are here too: https://github.com/junit-team/junit4/wiki/matchers-and-assertthat

Upvotes: 4

Related Questions