How to compare a list containing another list in junit 5?

Viewed 568

I have three classes as follows :

public class Foo {
 private Long fooId;
 private String fooName;
 private List<Bar> bars;
}

public class Bar  {
    private Long barId;
    private String barName;
    private Foo foo;
}

public class FooBar {
    List<Foo> foo;
    List<Bar> bars;
}

FooBar is what I am getting in the response. I am little bit confused about how to compare, especially Foo using hasItem() of hamcrest.Matchers.* or is there any better way. how we can compare this in Junit5?

1 Answers

First of all, your Foo and Bar should have something in common, a basis for comparison, by which it will be possible to compare them.

If they do not have anything in common, I do not see any way to compare incomparable objects.

As long as your Foo and Bar have something in common, or at least they override.equals() and implement their own ones which would enable comparison of these two (can mutually compare these two types), you can use:

assertIterableEquals(expected, actual); //from org.junit.jupiter.api.Assertions

This method asserts whether two iterables are deeply equal.

Note, that if both expected and actual are nulls, they are considered equal.

Related