junit 我无法从测试类访问实体类的Lombok功能

jogvjijk  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(200)

我在src/main/java目录中有一个实体类,如下所示:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
    private String firstName;
    private String lastName;

    @Column(name = "email_address",
    nullable = false
    )
    private String emailId;
    private String gaurdianName;
    private String gaurdianEmail;
    private String gaurdianMobile;

}

在src/test/java目录下的测试类中,我无法访问上面类中的Lombok特性,如setter和getter、builder方法和Lombok的其他特性。
问题出在哪里?
在我的测试类中,我想用builder方法或getter和setter方法创建一个student类的对象,如下所示:

Student student = new Student();

student.setFirstName("John"); // it doesn't work
student.getFirstName(); // it doesn't work
Student student = Student.builder().firstName("JOHN").build(); // it doesn't work

下面是我的pom.xml:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
l2osamch

l2osamch1#

您应该在maven文件中将提供的添加到Lombok依赖项中。这也将为测试类路径启用此依赖项。如以下文档所示
https://projectlombok.org/setup/maven

vvppvyoh

vvppvyoh2#

首先检查您是否安装了lombok插件并启用了在IDE设置中启用注解处理,然后检查您的pom文件和lombok依赖项范围。应该提供

相关问题