import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.equalTo; assertThat(actual, hasItem(hasProperty("id", equalTo(1L))));
where actual is a POJO with id as Long.
I get,
The method assertThat(T, Matcher<? super T>) in the type MatcherAssert is not applicable for the arguments (List, Matcher<Iterable<? super Object>>)
From various documentation and other stackoverflow pages, it should be valid, but I get the above error.
Hamcrest is used for unit testing in Java. The goal of Hamcrest is to make it easier to read and write test cases. We use Hamcrest to write the matcher objects that allow us to define the match rules declarative.
Class StringContains. Tests if the argument is a string that contains a substring. Creates a matcher that matches if the examined String contains the specified String anywhere.
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.
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.
Try explicitly filling in the type parameter - assuming actual
is a List<YourPojo>
, try calling:
assertThat(actual, hasItem(Matchers.<YourPojo>hasProperty("id", equalTo(1L))));
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