How can I assert hasProperty with a Java Record?

11

I have a piece of code in a test that checks that a list of results contains certain properties, using Hamcrest 2.2:

assertThat(result.getUsers(), hasItem(
    hasProperty("name", equalTo(user1.getName()))
));
assertThat(result.getUsers(), hasItem(
    hasProperty("name", equalTo(user2.getName()))
));

This worked perfectly fine when NameDto was a normal class. But after I changed it to a Record, Hamcrest's hasProperty complains about there being no property named name:

java.lang.AssertionError:
Expected: a collection containing hasProperty("name", "Test Name")
     but: mismatches were: [No property "name", No property "name"]

Is there some other matcher I can use to achieve the same matching as before? Or some other workaround I can use to get it to work with records?

Share
Improve this question

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

Chart JS +ng2-charts not working on Angular+2