How to compare recursively ignoring given fields using assertJ?

Tarun Maganti picture Tarun Maganti · Apr 10, 2017 · Viewed 19.1k times · Source

AssertJ has isEqualToIgnoringGivenFields and isEqualToComparingFieldByFieldRecursively.

But, there is no way I can compare two objects recursively by ignoring some fields. As per this discussion, it must be in development.

How to still get my assert's return value to be compared recursively but ignoring some fields. Is it possible in any other library or can I do it somehow using AssertJ?

Answer

Ovidiu Nistor picture Ovidiu Nistor · Mar 10, 2019

With latest 'Recursive comparison api improvements' from AssertJ release 3.12.0 it's possible now to do a recursive comparison and ignore fields:

Assertions.assertThat(objActual)
                .usingRecursiveComparison()
                .ignoringFields("uniqueId", "otherId")
                .ignoringFieldsMatchingRegexes(".*someId")
                .ignoringOverriddenEqualsForTypes(MyData.class, MyDataItem.class)
                .isEqualTo(objExpected);