I want to compare two objects (DTO and an Entity) using AssertJ's recursive comparison, for purpose of unit-testing DTO->Entity mapper. Both of these objects have fields with pretty much the same values, but some of the fields have different names between these two objects. That results in following error:
DTO can't be compared to Entity as Entity does not declare all DTO fields, it lacks these: [dtoFieldA, dtoFieldB, dtoFieldC]
I've tried following configuration, but without luck:
assertThat(DTO)
.usingRecursiveComparison(RecursiveComparisonConfiguration.builder()
.withEqualsForFields(fieldAPRedicate, "dtoFieldA")
.withEqualsForFields(fieldBPRedicate, "dtoFieldB")
.withEqualsForFields(fieldCPRedicate, "dtoFieldC")
.build())
.isEqualTo(Entity)
BiPredicate<DTO, Entity> fieldAPRedicate = (d, e) -> d.dtoFieldA().equals(e.entityFieldA());
BiPredicate<DTO, Entity> fieldBPRedicate = (d, e) -> d.dtoFieldB().equals(e.entityFieldB());
BiPredicate<DTO, Entity> fieldCPRedicate = (d, e) -> d.dtoFieldC().equals(e.entityFieldC());
Compared classess:
DTO:
public class DTO {
protected String sameNameField1;
protected String sameNameField2;
protected String dtoFieldA;
protected String dtoFieldB;
protected String dtoFieldC;
}
Entity:
public class Entity {
protected String sameNameField1;
protected String sameNameField2;
protected String entityFieldA;
protected String entityFieldB;
protected String entityFieldC;
}
How to configure assertJ for such case?