0

I have the following structure

public class ComplexClazz {

    private List<A> a;
    private List<B> b;
}

public class A {
    private String someString;
    private List<C> c;
}

public class B {
   private BigDecimal someBigDecimal;
}

public class C {
   private String someString;
   private D d;
}

public class D {
   private String someString;
   private Integer someInt;
}

none of the classes implements equals. I would like to compare two ComplexClazzs for equality independent of the ordering in the lists.

In my legacy code this is so far solved with

ReflectionAssert.assertReflectionEquals(expectedResult, actualResult, ReflectionComparatorMode.LENIENT_ORDER);

but I would like to get rid of the outdated unitils library and use e.g. assertj.

I tried with assertThat(actualResult).containsExactlyInAnyOrder(expectedResult); in combination with usingFieldByFieldElementComparator but could not make it work.

Any ideas how to compare theses objects?

Marius
  • 365
  • 4
  • 18
  • 2
    Is there a reason why you don't want to add an equals()? – Mr R Mar 09 '21 at 08:34
  • 2
    You have already provided the answer on your question: Just implement `equals`. – Victor Polo De Gyves Montero Mar 09 '21 at 08:38
  • we will need some more info for this to be answerable. Can you implement the `equals` method? If not, do you have access to getters so that you can create some kind of equals strategy? – Yoni Mar 09 '21 at 08:40
  • Is there a reason why you don't want to add an equals()/hashCode/and a Comparator? With all those then it's much easier to use lots of other utilities ... What about [Apache Commons Collections CollectionUtils.isEqualCollection](http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEqualCollection-java.util.Collection-java.util.Collection-org.apache.commons.collections4.Equator-) for comparing your different order collections? – Mr R Mar 09 '21 at 08:44

1 Answers1

2

Give a try to AssertJ recursive comparison and use ignoringCollectionOrder, ex:

public class Person {
   String name;
   List<Person> friends = new ArrayList<>();
   // no equals method
 }

 Person sherlock1 = new Person("Sherlock Holmes");
 sherlock1.friends.add(new Person("Dr. John Watson"));
 sherlock1.friends.add(new Person("Molly Hooper"));

 Person sherlock2 = new Person("Sherlock Holmes");
 sherlock2.friends.add(new Person("Molly Hooper"));
 sherlock2.friends.add(new Person("Dr. John Watson"));

 // assertion succeeds as friends collection order is ignored in the comparison
 assertThat(sherlock1).usingRecursiveComparison()
                      .ignoringCollectionOrder()
                      .isEqualTo(sherlock2);

 // assertion fails as friends collection order is not ignored in the comparison
 assertThat(sherlock1).usingRecursiveComparison()
                      .isEqualTo(sherlock2);
Joel Costigliola
  • 6,308
  • 27
  • 35
  • Thanks a lot! It works, just had to add `.withComparatorForType(new BigDecimalComparator(), BigDecimal.class)` because I have a `BigDecimal` in the structure. – Marius Mar 11 '21 at 10:26