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

G.S
  • 10,413
  • 7
  • 36
  • 52
wenic
  • 1,169
  • 2
  • 14
  • 23
  • Please see "[ask]" and the linked pages and "[MCVE]". What is the minimal code that demonstrates the problem you encountered, along with the explanation of the problem, and the minimal input data and the expected result? We need to be able to duplicate the problem, and this example code won't do that. – the Tin Man Feb 19 '22 at 20:57

2 Answers2

63

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

assertThat(actual, hasItem(Matchers.<YourPojo>hasProperty("id", equalTo(1L))));
pobrelkey
  • 5,853
  • 20
  • 29
  • thanks, that works. We had a similar issue when using containsInAnyOrder but couldn't get it to work. – wenic Nov 20 '13 at 18:46
11

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

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