wenic wenic - 4 months ago 32
Java Question

hamcrest hasItem and hasProperty, assert if a object with property value exists

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
is not applicable for the arguments
(List<Pojo>, Matcher<Iterable<? super Object>>)

From various documentation and other stackoverflow pages, it should be valid, but I get the above error.


Try explicitly filling in the type parameter - assuming actual is a List<YourPojo>, try calling:

assertThat(actual, hasItem(Matchers.<YourPojo>hasProperty("id", equalTo(1L))));