mardi 9 mars 2021

Compare two lists with elements field-by-field

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 int someInt;
}

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?

Aucun commentaire:

Enregistrer un commentaire