Search code examples
javajpaassertj

Can AssertJ isEqualToComparingFieldByFieldRecursively ignore a field?


I need to compare a JPA Entity with expected value ignoring IDs. AssertJ has this method

assertThat(actualEntity).isEqualToIgnoringGivenFields(expectedEntity, "id");

which compares two objects ignoring ID, but my actualEntity has a list of inner entities, that have IDs too. Is there a method that would recursively ignore field id in the inner objects as well?

I saw there is this method

assertThat(actuactualEntitya).isEqualToComparingFieldByFieldRecursively(expectedEntity);

but I don't see an option how to tell that I want to ignore id recursively. Is there any way, how to achieve that using AssertJ?


Solution

  • Yes it can, use the recursive comparison for that https://assertj.github.io/doc/#assertj-core-recursive-comparison which lets you specify fields to ignore by name or regex: https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-fields