mercredi 17 février 2021

Hamcrest assert complex object with List member

If I have a POJO class

  @Value
  @AllArgsConstructor
  class Person {
    private final String name;
    private final int age;
    private final List<Address> addresses;
  }

As you can see, the object contains a List. In my unit tests, it is hard to assert on this since the ordering is not known of the addresses (or any other potential List/Set/Map property of Person).

How can I do the assertion without changing the equals method of the Person class and make sure the ordering of addresses is ignored? I'm looking for something like

assertThat(actualPerson, is(expectedPerson).ignoreOrderingOf("addresses"));

What I mean is that the ordering of actualPerson.addresses and expectedPerson.addresses should be ignored in the assertion.

What testing strategy should I use in general to solve this issue?

Aucun commentaire:

Enregistrer un commentaire