Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest. Match item in collection with 2 specific property values

I have a test with a collection of SpecialObject as result. SpecialObject has "name" and "surname" as properties. I want to test if the collection contains a specialObject with 2 specific properties, "name=myname" and "surname=lastname".

Here is what I have tried without success:

assertThat(result, Matchers.<SpecialObject>hasItem(
    allOf(
          hasProperty("name", equalTo("myname")),
          hasProperty("surname", equalTo("lastname"))
));
like image 871
Slagathor Avatar asked Oct 11 '25 07:10

Slagathor


1 Answers

You can use both matcher to check if both properties has excepcted values.

    Assert.assertThat(result, Matchers.<SpecialObject>hasItem(
            Matchers.both(hasProperty("name", equalTo("myname")))
                    .and(hasProperty("surname", equalTo("lastname")))));
like image 176
Vlad Bochenin Avatar answered Oct 13 '25 19:10

Vlad Bochenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!