英文:
Assertj: How to compare 2 objects list by objects content?
问题
以下是翻译好的部分:
给定以下(快速且不完整)代码:
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().isEqualTo(l2.get(i));
}
英文:
Given the following (quick and missing) code:
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));
How can I compare the content of the lists?
Everything I used so far checked if the objects themselves were equal and not the objects value:
assertThat(l1).isEqualTo(l2);
assertThat(l1).containsAll(l2);
assertThat(l1).containsExactly(values);
assertThat(l1).containsExactlyElementsOf(iterable);
Must I implement equals() method for Match class?
May this be the correct way?
for (int i = 0; i < l1.size(); i++){
assertThat(l1.get(i)).usingRecursiveComparison().isEqualTol2.get(i));
}
答案1
得分: 14
尝试使用 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();
// 断言成功,因为两个列表按顺序包含相等的项目。
assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)
.contains(sheldon);
有关更详细的解释,请参阅 https://assertj.github.io/doc/#assertj-core-recursive-comparison-for-iterable。
英文:
Give a try to usingRecursiveFieldByFieldElementComparator(recursiveConfiguration)
, it enables recursive comparison to all iterable assertions.
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 for a more detailed explanation.
答案2
得分: 6
你应该重写 equals()
和 hashCode()
方法。
英文:
You should override equals()
and hashCode()
答案3
得分: 2
是的,因此经过进一步的研究,我建议:
for (int i = 0; i < l1.size(); i++){
assertThat(l1.get(i)).usingRecursiveComparison().isEqualTo(l2.get(i));
}
你可以阅读详细信息:
https://assertj.github.io/doc/#assertj-core-recursive-comparison
英文:
Yes, so after further researching I would recommend:
for (int i = 0; i < l1.size(); i++){
assertThat(l1.get(i)).usingRecursiveComparison().isEqualTol2.get(i));
}
You can read the details:
https://assertj.github.io/doc/#assertj-core-recursive-comparison
答案4
得分: 1
使用<a href="https://joel-costigliola.github.io/assertj/index.html">AssertJ</a>:
assertThat(actualModel)
.usingRecursiveComparison()
...各种其他选项()
.isEqualTo(expectedModel);
英文:
Using <a href="https://joel-costigliola.github.io/assertj/index.html">AssertJ</a>:
assertThat(actualModel)
.usingRecursiveComparison()
...variousOtherOptions()
.isEqualTo(expectedModel);
答案5
得分: 0
@vincentdep 是正确的。如果你正在使用 Java 14 或更高版本,你可以使用 record
类:
public record Pair(int x, int y){};
List l1 = Arrays.asList(new Pair(1,2), new Pair(1,3), new Pair(2,3));
List l2 = Arrays.asList(new Pair(1,2), new Pair(1,3), new Pair(2,3));
assertThat(l1).isEqualTo(l2);
assertThat(l1).containsAll(l2);
英文:
@vincentdep is correct. If you are using Java 14 or above, you can use a record
class:
public record Pair(int x, int y){};
List l1 = Arrays.asList(new Pair(1,2), new Pair(1,3), new Pair(2,3));
List l2 = Arrays.asList(new Pair(1,2), new Pair(1,3), new Pair(2,3));
assertThat(l1).isEqualTo(l2);
assertThat(l1).containsAll(l2);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论