我想检查一个列表是否包含另一个列表的子列表。这是我目前如何做的一个虚拟示例:
@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;
}
}
equals
是否可以在不覆盖类方法的情况下以更紧凑的方式实现相同的结果User
?我正在寻找类似的东西:
assertThat(users).contains(newUsers.get(0), newUsers.get(1)).extracting("id");
或者,更好的是:
assertThat(users).contains(newUsers.subList(0, 2)).extracting("id");