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

wenic picture wenic · Nov 20, 2013 · Viewed 29.5k times · Source
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<Pojo>, Matcher<Iterable<? super Object>>)

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

Answer

pobrelkey picture pobrelkey · Nov 20, 2013

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

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