Trying to compile this code
import static org.hamcrest.Matchers.is;
import static org.hamcrest.number.OrderingComparison.lessThan;
...
Assert.assertThat(0, is(lessThan(1)));
issues this compilation error:
assertThat(Object, org.hamcrest.Matcher<java.lang.Object>)
cannot be applied to(int, org.hamcrest.Matcher<capture<? super java.lang.Integer>>)
Could be this collisions between different hamcrest versions? I'm using jUnit 4.6 and hamcrest 1.3
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”.
Purpose of the Hamcrest matcher framework. 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.
Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.
I believe the problem is that JUnit comes bundled with an older copy of Hamcrest (1.1) as signatures in later version of Hamcrest are incompatible with JUnit. There are two possible solutions:
org.junit.Assert.assertThat()
to org.hamcrest.MatcherAssert.assertThat()`.The latter is probably my recommended option, since the Hamcrest version of assertThat()
produces nicer failure messages, and versions later than 1.1 have some nice features (e.g. TypeSafeDiagnosingMatcher
).
I don't use Hamcrest, but obviously int
isn't an Object. Use Integer
instead, e.g.
Assert.assertThat(Integer.valueOf(0), is(lessThan(1)));
I suppose you are using Java version <= 1.4 where auto-boxing doesn't work. Hence you need an explicit conversion to Integer
first.
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