Search code examples
javaasserthamcrest

Hamcrest: How to test for multiple properties of an object


rules=[{type:"path", value:"abc"},{type:"cookie", value:"xyz"}, ...]

I want to find if the array contains an object with properties (type=path and value=abc)

I tried something like this: assertThat(rules, hasItem(hasProperty("type", equals("path"))));

But I didn't find a way to combine two hasProperty methods. Can someone help me


Solution

  • The following will try to apply each Matcher in the allOf() check to each item in rules:

        assertThat(rules,
                hasItem(allOf(hasProperty("type", equalTo("path")),
                        hasProperty("value", equalTo("abc")))));