Setter 在 DTO 中只是为了集成测试?

Setter in DTO just for integration testing?

我找不到我正在寻找的信息,因此在此处发帖寻求建议并了解更好的方法。

我有一个不可变的 DTO 对象,例如:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDate;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AccountCreateDto {
    String name;
    Long accountNo;
    String email;
    LocalDate dob;
    String address;
    Long phone;
    Integer amount;
    LocalDate accountOpeningDate;
    Integer installmentAmount;
    Integer totalAmount;
    LocalDate installmentPaidDate;

    @JsonCreator
    public AccountCreateDto(
            @JsonProperty("name") String name,
            @JsonProperty("accountNo") Long accountNo,
            @JsonProperty("email") String email,
            @JsonProperty("dob") LocalDate dob,
            @JsonProperty("address") String address,
            @JsonProperty("phone") Long phone,
            @JsonProperty("amount") Integer amount,
            @JsonProperty("accountOpeningDate") LocalDate accountOpeningDate,
            @JsonProperty("installmentAmount") Integer installmentAmount,
            @JsonProperty("totalAmount") Integer totalAmount,
            @JsonProperty("installmentPaidDate") LocalDate installmentPaidDate
    ) {
        this.name = name;
        this.accountNo = accountNo;
        this.email = email;
        this.dob = dob;
        this.address = address;
        this.phone = phone;
        this.amount = amount;
        this.accountOpeningDate = accountOpeningDate;
        this.installmentAmount = installmentAmount;
        this.totalAmount = totalAmount;
        this.installmentPaidDate = installmentPaidDate;
    }
    // removing getters for brevity
}

现在我正在使用 testcontainer 进行集成测试,我希望 accountOpeningDateinstallmentPaidDate 成为动态值,因此为了从集成测试中设置这些值,这是一个很好的选择吗?仅向此 DTO 添加 2 个设置器的想法,仅用于如下所示的集成测试?

    public void setAccountOpeningDate(LocalDate accountOpeningDate) {
        this.accountOpeningDate = accountOpeningDate;
    }

    public void setInstallmentPaidDate(LocalDate installmentPaidDate) {
        this.installmentPaidDate = installmentPaidDate;
    }

或者有比这更好的方法吗?寻找建议。

TA

您将失去对象的不变性,这是不可接受的。使用不可变 类,我们无法修改状态,但必须创建一个具有变异状态的新实例。
构建器模式在这种情况下非常有用。
示例:

AccountCreateDto existingImmutableDTO = new AccountCreateDto("name", 1L, "name@aaa", LocalDate.now(), "Address", null, null, null, null, null, null );
        
//change state of imuttable object, create new instance via builder
AccountCreateDto modifiedImmutableDTO = new AccountCreateDtoBuilder(existingImmutableDTO).setAccountOpeningDate(LocalDate.now()).setInstallmentPaidDate(LocalDate.now()).build();
public class AccountCreateDtoBuilder {
    private String name;
    private Long accountNo;
    private String email;
    private LocalDate dob;
    private String address;
    private Long phone;
    private Integer amount;
    private LocalDate accountOpeningDate;
    private Integer installmentAmount;
    private Integer totalAmount;
    private LocalDate installmentPaidDate;

    public AccountCreateDtoBuilder() {
        
    }

    public AccountCreateDtoBuilder(AccountCreateDto accountCreateDto) {
        this.name = accountCreateDto.getName();
        this.accountNo = accountCreateDto.getAccountNo();
        this.email = accountCreateDto.getEmail();
        this.dob = accountCreateDto.getDob();
        this.address = accountCreateDto.getAddress();
        this.phone = accountCreateDto.getPhone();
        this.amount = accountCreateDto.getAmount();
        this.accountOpeningDate = accountCreateDto.getAccountOpeningDate();
        this.installmentAmount = accountCreateDto.getInstallmentAmount();
        this.totalAmount = accountCreateDto.getTotalAmount();
        this.installmentPaidDate = accountCreateDto.getInstallmentPaidDate();
    }

    public AccountCreateDto build() {
        return new AccountCreateDto(name, accountNo, email, dob, address, phone, amount, accountOpeningDate, installmentAmount, totalAmount, installmentPaidDate);
    }

    public AccountCreateDtoBuilder setName(String name) {
        this.name = name;
        return this;
    }

    public AccountCreateDtoBuilder setAccountNo(Long accountNo) {
        this.accountNo = accountNo;
        return this;
    }

    public AccountCreateDtoBuilder setEmail(String email) {
        this.email = email;
        return this;
    }

    public AccountCreateDtoBuilder setDob(LocalDate dob) {
        this.dob = dob;
        return this;
    }

    public AccountCreateDtoBuilder setAddress(String address) {
        this.address = address;
        return this;
    }

    public AccountCreateDtoBuilder setPhone(Long phone) {
        this.phone = phone;
        return this;
    }

    public AccountCreateDtoBuilder setAmount(Integer amount) {
        this.amount = amount;
        return this;
    }

    public AccountCreateDtoBuilder setAccountOpeningDate(LocalDate accountOpeningDate) {
        this.accountOpeningDate = accountOpeningDate;
        return this;
    }

    public AccountCreateDtoBuilder setInstallmentAmount(Integer installmentAmount) {
        this.installmentAmount = installmentAmount;
        return this;
    }

    public AccountCreateDtoBuilder setTotalAmount(Integer totalAmount) {
        this.totalAmount = totalAmount;
        return this;
    }

    public AccountCreateDtoBuilder setInstallmentPaidDate(LocalDate installmentPaidDate) {
        this.installmentPaidDate = installmentPaidDate;
        return this;
    }
}