
[英]AssertJ usingRecursiveComparison fails when a class field is a collection
[英]List diffs in the context of usingRecursiveComparison()
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
这是一个特定于 AssertJ 的问题。 考虑下面的测试,其中两个列表首先直接比较,然后通过usingRecursiveComparison()
嵌套在其他一些 object 中。
public class RecursiveComparisonWithNestedListsTest {
@Test
void topLevelList() {
assertThat(List.of("Bob", "Chris"))
.containsExactlyElementsOf(List.of("Bob"));
}
@Test
void nestedList() {
assertThat(new Person("Amy", List.of("Bob", "Chris")))
.usingRecursiveComparison()
.isEqualTo(new Person("Amy", List.of("Bob")));
}
}
record Person(String name, List<String> friends) {
}
在第一种情况下,消息更有帮助,指定缺少的元素:
Expecting actual:
["Bob", "Chris"]
to contain exactly (and in same order):
["Bob"]
but some elements were not expected:
["Chris"]
在第二种情况下,消息只是提到大小不同:
Expecting actual:
Person[name=Amy, friends=[Bob, Chris]]
to be equal to:
Person[name=Amy, friends=[Bob]]
when recursively comparing field by field, but found the following difference:
field/property 'friends' differ:
- actual value : ["Bob", "Chris"]
- expected value: ["Bob"]
actual and expected values are collections of different size, actual size=2 when expected size=1
这只是一个简单的例子; 实际用例涉及更复杂的对象和更大的列表。 在这种情况下,指定列表中缺少的项目会产生很大的不同。 这让我想到了我的问题:有没有办法像第一个例子那样配置递归比较来比较列表?
我当然有解决方法来忽略递归比较中的列表,然后单独声明列表相等性,但我想知道是否有一些技巧可以一步完成。
不,目前没有这种能力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.