This is an AssertJ specific question. Consider the tests below where two lists are compared first directly and then nested in some other object through usingRecursiveComparison()
.
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) {
}
In the first case the message is more helpful, specifying the missing element:
Expecting actual:
["Bob", "Chris"]
to contain exactly (and in same order):
["Bob"]
but some elements were not expected:
["Chris"]
In the second case the message just mentions that the size differs:
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
This is just a simple example; the actual use case involves more complex objects and much larger lists. In that context, specifying what item is missing from the list makes a big difference. This brings me to my question: Is there any way to configure the recursive comparison to compare lists just like in the first example?
I certainly have the workaround to ignore the list in the recursive comparison and then assert list equality separately, but I was wondering if there's some trick to get it all in one step.