Search code examples
javaunit-testinghamcrestassertj

Testing content of list ignoring some of the fields


I have a scenario where I receive a list from a method call and I would like to assert that the list contains the correct elements. One way to do this would be to look for some detail in each element to see which expected element to compare with - eg. a name. However the elements also contains a random generated UUID that I do not care about comparing.
And then I thought a test tool might come to my rescue. Take the following simplified example.

I have a class Dog:

public class Dog {
    private String name;
    private Integer age;
}

They are contained in a list:

List<Dog> dogs = ... many dogs

Now I want to test that the list contains the expected dogs, but for some reason I do not know some of the fields - let us say age. I have tried both using assertj and hamcrest but I cannot find the correct solution that both compares two lists while also ignoring some of the fields.

Until now this is what I have (using hamcrest):

List<Dog> actualDogs = codeUndertest.findDogs(new Owner("Basti"));
List<Dog> expectedDogs = createExpectedListOfDogsWithoutTheAge();

Matcher.assertThat(actualDogs.get(0), Matcher.is(com.shazam.shazamcrest.matcher.Matchers
    .sameBeanAs(expectedDogs.(0))
    .ignoring("age")
))

This works but it only compares two objects of class Dog. How do I compare all the dogs in the two list?
Bonus question: how do I compare the lists without knowing the order or if I only need to assert that the expected dogs are contained in the list.


Solution

  • The method mentioned above usingElementComparatorIgnoringFields is now deprecated. You should use usingRecursiveFieldByFieldElementComparatorIgnoringFields instead of it. For example:

    @Test
    public void shouldCompareDogsWithoutAge() {
        final List<Dog> actual = List.of(new Dog("Azor", null), new Dog("Rex", null));
        assertThat(actual)
            .usingRecursiveFieldByFieldElementComparatorIgnoringFields("age")
            .containsExactlyInAnyOrder(new Dog("Azor", 12), new Dog("Rex", 9));
    }
    
    public class Dog {
        private String name;
        private Integer age;
    
        public Dog(final String name, final Integer age) {
            this.name = name;
            this.age = age;
        }
    }