Search code examples
javajunithamcrest

Multiple hasProperty constraints for Hamcrest matcher


I tried the following code, for matching a single ComplexObject in a List

assertThat(complexObjectList, Matchers.<ComplexObject>hasItems(
    hasProperty("lang", equalTo(lang)), 
    hasProperty("name", equalTo(name)),
    hasProperty("desc", equalTo(desc)));

I want to have a filter of

match(lang) && match(name) && match(desc)

but with the above code, i get

match(lang) || match(name) || match(desc)

How can I verify those three different hasProperty matchers?


Solution

  • You can use the allOf matcher.

    assertThat(complexObjectList,
      Matchers.<ComplexObject>hasItem(allOf(
        hasProperty("lang", equalTo(lang)),
        hasProperty("name", equalTo(name)),
        hasProperty("desc", equalTo(desc))));