给出以下(快速且缺失的)代码:
class Pair{
int x;
int y;
}
List l1 = Arrays.asList(new Match(1,2), new Match(1,3), new Match(2,3));
List l2 = Arrays.asList(new Match(1,2), new Match(1,3), new Match(2,3));
如何比较列表的内容?
到目前为止我使用的所有内容都检查对象本身是否相等而不是对象值:
assertThat(l1).isEqualTo(l2);
assertThat(l1).containsAll(l2);
assertThat(l1).containsExactly(values);
assertThat(l1).containsExactlyElementsOf(iterable);
我必须为 Match 类实现 equals() 方法吗?
这可能是正确的方法吗?
for (int i = 0; i < l1.size(); i++){
assertThat(l1.get(i)).usingRecursiveComparison().isEqualTol2.get(i));
}
尝试一下usingRecursiveFieldByFieldElementComparator(recursiveConfiguration)
,它可以对所有可迭代断言进行递归比较。
Ex:
public class Person {
String name;
boolean hasPhd;
}
public class Doctor {
String name;
boolean hasPhd;
}
Doctor drSheldon = new Doctor("Sheldon Cooper", true);
Doctor drLeonard = new Doctor("Leonard Hofstadter", true);
Doctor drRaj = new Doctor("Raj Koothrappali", true);
Person sheldon = new Person("Sheldon Cooper", false);
Person leonard = new Person("Leonard Hofstadter", false);
Person raj = new Person("Raj Koothrappali", false);
Person howard = new Person("Howard Wolowitz", false);
List<Doctor> doctors = list(drSheldon, drLeonard, drRaj);
List<Person> people = list(sheldon, leonard, raj);
RecursiveComparisonConfiguration configuration = RecursiveComparisonConfiguration.builder()
.withIgnoredFields("hasPhd")
.build();
// assertion succeeds as both lists contains equivalent items in order.
assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)
.contains(sheldon);
See https://assertj.github.io/doc/#assertj-core-recursive-comparison-for-iterable https://assertj.github.io/doc/#assertj-core-recursive-comparison-for-iterable以获得更详细的解释。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)