给出以下(快速且缺失的)代码:
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));
Run Code Online (Sandbox Code Playgroud)
如何比较列表的内容?到目前为止我使用的所有内容都检查对象本身是否相等而不是对象值:
assertThat(l1).isEqualTo(l2);
assertThat(l1).containsAll(l2);
assertThat(l1).containsExactly(values);
assertThat(l1).containsExactlyElementsOf(iterable);
Run Code Online (Sandbox Code Playgroud)
我必须为 Match 类实现 equals() 方法吗?
这可能是正确的方法吗?
for (int i = 0; i < l1.size(); i++){
assertThat(l1.get(i)).usingRecursiveComparison().isEqualTol2.get(i));
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*ola 15
尝试一下usingRecursiveFieldByFieldElementComparator(recursiveConfiguration),它可以对所有可迭代断言进行递归比较。
前任:
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);
Run Code Online (Sandbox Code Playgroud)
有关更详细的说明,请参阅https://assertj.github.io/doc/#assertj-core-recursive-comparison-for-iterable 。
| 归档时间: |
|
| 查看次数: |
19117 次 |
| 最近记录: |