1

I saw this post

assertThat( myClass.getMyItems(), contains(
    hasProperty("foo", is("bar")), 
    hasProperty("name", is("bar"))
));

and

  assertThat(logsFromWaze, hasItem(
                hasProperty("foo", is("bar")),
                hasProperty("name", is("bar"))));

how can it work?

doesn't hasItem expect one matcher as an argument and not a list of matchers?

Community
  • 1
  • 1
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

1 Answers1

2

There is no hasItem matcher that accepts varargs, but you can combine both hasProperty("foo", is("bar")) and hasProperty("name", is("bar")) via allOf matcher:

assertThat(logsFromWaze, hasItem(
        allOf(
                hasProperty("foo", is("bar")),
                hasProperty("name", is("bar"))
        )
));

This test will succeed when a single pass over the examined Iterable yields at least one item that matches all of the matchers passed to allOf.

Constantine
  • 3,187
  • 20
  • 26