Search code examples
javaassertj

AssertJ how to compare two list by their element value?


Suppose I have a list of User userList.

class User {
    String firstName;
    String lastName;
}

Is there a way I can compare using AssertJ like

assertThat(userList).lastName.equalsTo(List.of("A","B","C"));

?


Solution

  • List<User> users = Arrays.asList(
            new User("a", "A"),
            new User("b", "B"),
            new User("c", "C"));
    
    assertThat(users)
            .extracting(User::getLastName)
            .containsExactly("A", "B", "C");