assertj:比较 dto 和实体之间的字段 类

assertj: compare fields between dto and entity classes

我需要比较 DTO class 和它的 Entity class。

例如,AddressDTO class 将是:

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class AddressDTO {

    private StringTypeDTO text;
    private List<StringTypeDTO> line;
    private StringTypeDTO city;
    private StringTypeDTO district;
    private StringTypeDTO state;
    private StringTypeDTO postalCode;
    private StringTypeDTO country;

}

我的 AddressEntity class 将是:

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Address {

    private StringType text;
    private List<StringType> line;
    private StringType city;
    private StringType district;
    private StringType state;
    private StringType postalCode;
    private StringType country;

}

有什么方法可以使用 assertj 来比较这两个对象吗?

StringType:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class StringTypeDTO {

    private String value;
    
}

StringType

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class StringType {

    private String value;
    
}

我想避免编写如下代码:

assertThat dto.field1 is equals to entity.field1 of object
assertThat dto.field2 is equals to entity.field2 of object
assertThat dto.field3 is equals to entity.field3 of object

有什么想法吗?

field-by-field recursive comparison就是您要找的:

AddressDTO addressDTO = new AddressDTO(...);
AddressEntity addressEntity = new AddressEntity(...);

assertThat(addressDTO).usingRecursiveComparison()
                      .isEqualTo(addressEntity);