wenic
wenic

Reputation: 1189

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 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.

Upvotes: 36

Views: 39168

Answers (2)

Cyva
Cyva

Reputation: 1137

The shorter version when you do not have to specify class type:

List<IssueDefinitionDto> definitions = ...; // Tested variable
...
assertThat(definitions, hasItem(hasProperty("id", is(10L))));

Upvotes: 13

pobrelkey
pobrelkey

Reputation: 5973

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

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

Upvotes: 65

Related Questions