
[英]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.