使用 Lombok @Builder 注解的多个构造函数会导致问题吗?

问题描述 投票:0回答:1

这是我的第一个问题,所以请随时纠正我在写这个问题时错过的任何内容:)

我在将 @Bulider 与我的 DTO 对象一起使用时遇到问题。 我有一个抽象类“EventDTO”,由类类型“CreateTaskLogDTO”继承。

我在两个类上各有两个构造函数。 CreateTaskLogDTO 中的构造函数继承了 EventDTO 中的构造函数。

奇怪的是,构造函数用
注释 @Builder(builderMethodName = "buildWithSpecificParamsNoID") 工作正常,但与 @Builder(builderMethodName = "buildWithOperationStringID") 无法实例化以下字段:String OperationString、Long id

EventDTO(父类)源代码

package com.wwme.wwme.log.domain.DTO;

import com.wwme.wwme.group.domain.Group;
import com.wwme.wwme.log.domain.OperationType;
import com.wwme.wwme.task.domain.Task;
import com.wwme.wwme.user.domain.User;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

@Getter
@Setter
public abstract class EventDTO {

    public EventDTO(Long id, User user, Group group, OperationType operationTypeEnum, LocalDateTime operationTime, String operationString, Task task) {
        this.id = id;
        this.user = user;
        this.group = group;
        this.operationTypeEnum = operationTypeEnum;
        this.operationTime = operationTime;
        this.operationString = operationString;
        this.task = task;
    }

    public EventDTO(User user, Group group, OperationType operationTypeEnum, LocalDateTime operationTime, Task task) {
        this.user = user;
        this.group = group;
        this.operationTypeEnum = operationTypeEnum;
        this.operationTime = operationTime;
        this.task = task;
    }

    protected Long id;
    protected User user;
    protected Group group;
    protected Task task;
    protected OperationType operationTypeEnum;
    protected LocalDateTime operationTime;
    protected String operationString;


    //Calculate Operation String based on given parameters
    public abstract void setOperationStr();

    @Override
    public String toString() {
        return "EventDTO{" +
                "id=" + id +
                ", user=" + user.getNickname() +
                ", group=" + group.getGroupName() +
                ", task=" + task.getTaskName() +
                ", operationTypeEnum=" + operationTypeEnum +
                ", operationTime=" + operationTime +
                ", operationString='" + operationString + '\'' +
                '}';
    }
    //set specific fields for 
    public abstract void setSpecificFields();
    public abstract String convertToString();
}

CreateTaskLogDTO(子类)的源代码

package com.wwme.wwme.log.domain.DTO;

import com.wwme.wwme.group.domain.Group;
import com.wwme.wwme.log.domain.OperationType;
import com.wwme.wwme.task.domain.Task;
import com.wwme.wwme.user.domain.User;
import lombok.*;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Slf4j
@Getter
public class CreateTaskLogDTO extends EventDTO{
    private String newTaskName;

    @Builder(builderMethodName = "buildWithSpecificParamsNoID")
    public CreateTaskLogDTO(User user, Group group, OperationType operationTypeEnum, LocalDateTime operationTime, String newTaskName, Task task) {
        super(user, group, operationTypeEnum, operationTime, task);
        this.newTaskName = newTaskName;
        setOperationStr();
    }

    @Builder(builderMethodName = "buildWithOperationStringID") //buildWithOperationStringID
    public CreateTaskLogDTO(Long id, User user, Group group, OperationType operationTypeEnum, LocalDateTime operationTime, String operationString, Task task) {
        super(id, user, group, operationTypeEnum, operationTime, operationString, task);
        setSpecificFields();
    }

    @Override
    public String toString() {

        return super.toString() +  "CreateTaskLogDTO{" +
                "newTaskName='" + newTaskName + '\'' +
                ", operationString='" + operationString + '\'' +
                '}';
    }

    @Override
    public void setOperationStr() {
        this.operationString = newTaskName;
    }

    @Override
    public void setSpecificFields() {
        this.newTaskName = this.getOperationString();
    }

    @Override
    public String convertToString() {
        log.info(this.getUser().getNickname());
        log.info(this.getNewTaskName());

        return "\"" + this.getUser().getNickname() + "\" 님이 " + "\"" + this.getNewTaskName() +"\""
                + "과제를 생성하였습니다.";

    }


}

谢谢!

我尝试运行测试代码来正确识别问题。

这是不起作用的测试(使用 buildWithOperationStringID 构建器方法) 它无法设置字段:Id 和 OperationString。其他一切都设置得很好。

   @Test
    void createTaskLogDTO_builder_withID_test(){
        String operationString = "Task1";
        LocalDateTime now = LocalDateTime.now();

        when(mockTask.getTaskName()).thenReturn("Task1");
        when(mockUser.getNickname()).thenReturn("User1");
        when(mockGroup.getGroupName()).thenReturn("Group1");

        CreateTaskLogDTO createTaskLogDTO =  CreateTaskLogDTO.buildWithOperationStringID()
                .id(1L)
                .task(mockTask)
                .user(mockUser)
                .group(mockGroup)
                .operationString(operationString)
                .operationTime(now)
                .operationTypeEnum(OperationType.CREATE_TASK)
                .build();

        assertEquals(createTaskLogDTO.getId(),1L);
        assertEquals(createTaskLogDTO.getTask().getTaskName(),mockTask.getTaskName());
        assertEquals(createTaskLogDTO.getUser().getNickname(),mockUser.getNickname());
        assertEquals(createTaskLogDTO.getGroup().getGroupName(),mockGroup.getGroupName());
        assertEquals(createTaskLogDTO.getOperationTime(),now);
        assertEquals(createTaskLogDTO.getOperationTypeEnum(),OperationType.CREATE_TASK);
        assertEquals(operationString,createTaskLogDTO.getOperationString());
    }

这是对有效构建器方法的测试(buildWithSpecificParamsNoID) 它正确设置所有字段。

    @Test
    void createTaskLogDTO_build_withoutID_test(){
        when(mockTask.getTaskName()).thenReturn("Task1");
        when(mockUser.getNickname()).thenReturn("User1");
        when(mockGroup.getGroupName()).thenReturn("Group1");

        String operationString = "Task1";
        LocalDateTime now = LocalDateTime.now();

        CreateTaskLogDTO createTaskLogDTO = CreateTaskLogDTO.buildWithSpecificParamsNoID()
                .task(mockTask)
                .user(mockUser)
                .group(mockGroup)
                .operationTime(now)
                .operationTypeEnum(OperationType.CREATE_TASK)
                .newTaskName(mockTask.getTaskName())
                .build();

        assertEquals(createTaskLogDTO.getTask().getTaskName(),mockTask.getTaskName());
        assertEquals(createTaskLogDTO.getUser().getNickname(),mockUser.getNickname());
        assertEquals(createTaskLogDTO.getGroup().getGroupName(),mockGroup.getGroupName());
        assertEquals(createTaskLogDTO.getOperationTime(),now);
        assertEquals(createTaskLogDTO.getOperationTypeEnum(),OperationType.CREATE_TASK);
        assertEquals(operationString,createTaskLogDTO.getOperationString());
        assertEquals(mockTask.getTaskName(),createTaskLogDTO.getNewTaskName());

    }

java inheritance constructor lombok builder
1个回答
0
投票

似乎

lombok
不支持一个类上有两个构建器。

如果您查看生成的

class
文件,您会发现它在两种
buildXXX
方法中都只使用了一个构建器。

但它不会产生错误。

您可以就此提交错误报告。

© www.soinside.com 2019 - 2024. All rights reserved.