lundi 5 juin 2017

Extracting field from both actual and expected objects when testing with AssertJ

I would like to check that a list contains a sub-list of another list. Here's a dummy example of how I do it currently:

@Test
public void testExtracting() throws Exception {
    final List<User> users = new ArrayList<>();
    users.add(new User(5234, "Adam"));
    users.add(new User(4635, "David"));

    final List<User> newUsers = new ArrayList<>();
    newUsers.add(new User(6143, "Bob"));
    newUsers.add(new User(3465, "Cindy"));

    users.addAll(newUsers);

    assertThat(users).extracting("id").contains(newUsers.get(0).id, newUsers.get(1).id);
}

public static class User {
    public long id;
    public String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

Is it possible to achieve the same result in a more compact way, without overriding the equals method of User class? I'm looking for something along the lines of:

assertThat(users).contains(newUsers.get(0), newUsers.get(1)).extracting("id");

Or, better yet:

assertThat(users).contains(newUsers.subList(0, 2)).extracting("id");

Aucun commentaire:

Enregistrer un commentaire