I have an actual
object that is an instance of List<List<<ExampleDTO>>
. I would like to assert that the nested lists are ordered.
To provide an example, let's say that the list contains of:
List<List<ExampleDTO>> actual = List.of(
List.of(
// id // name
new ExampleDTO(1, "Example1"),
new ExampleDTO(4, "Example4")
),
List.of(
new ExampleDTO(2, "Example2"),
new ExampleDTO(6, "Example6")
)
);
The example should pass as the nested lists are sorted by name ascending.
I'd like to create an assertion (using AssertJ library) that would make sure the NESTED lists are sorted according to name ascending. The isSortedAccordingTo
currently requires a Comparator<List<ExampleDTO>>
.
You could leverage allSatisfy
.
Assuming ExampleDTO
defined as:
public class ExampleDTO {
private final int id;
private final String name;
public ExampleDTO(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
your test case could be:
@Test
void test() {
List<List<ExampleDTO>> actual = List.of(
List.of(
// id // name
new ExampleDTO(1, "Example1"),
new ExampleDTO(4, "Example4")
),
List.of(
new ExampleDTO(2, "Example2"),
new ExampleDTO(6, "Example6")
)
);
assertThat(actual).allSatisfy(
list -> assertThat(list).isSortedAccordingTo(Comparator.comparing(ExampleDTO::getName))
);
}