[英]Compare two lists with elements field-by-field
我有以下结构
public class ComplexClazz {
private List<A> a;
private List<B> b;
}
public class A {
private String someString;
private List<C> c;
}
public class B {
private BigDecimal someBigDecimal;
}
public class C {
private String someString;
private D d;
}
public class D {
private String someString;
private Integer someInt;
}
没有一个类实现等于。 我想比较两个ComplexClazz
的相等性,而与列表中的顺序无关。
在我的遗留代码中,这到目前为止已解决
ReflectionAssert.assertReflectionEquals(expectedResult, actualResult, ReflectionComparatorMode.LENIENT_ORDER);
但我想摆脱过时的unitils
库并使用例如assertj。
我试过assertThat(actualResult).containsExactlyInAnyOrder(expectedResult);
结合usingFieldByFieldElementComparator
但无法使其工作。
任何想法如何比较这些对象?
尝试AssertJ 递归比较并使用ignoringCollectionOrder
,例如:
public class Person {
String name;
List<Person> friends = new ArrayList<>();
// no equals method
}
Person sherlock1 = new Person("Sherlock Holmes");
sherlock1.friends.add(new Person("Dr. John Watson"));
sherlock1.friends.add(new Person("Molly Hooper"));
Person sherlock2 = new Person("Sherlock Holmes");
sherlock2.friends.add(new Person("Molly Hooper"));
sherlock2.friends.add(new Person("Dr. John Watson"));
// assertion succeeds as friends collection order is ignored in the comparison
assertThat(sherlock1).usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(sherlock2);
// assertion fails as friends collection order is not ignored in the comparison
assertThat(sherlock1).usingRecursiveComparison()
.isEqualTo(sherlock2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.