Maven+Spring Boot项目测试类无法访问Lombok生成的Getters/Setters
Lombok @Data注解未生成getPassword()方法,编译报错"cannot find symbol"
错误详情
在基于Maven的Spring Boot项目中,User类添加了@Data注解预期生成属性的Getters和Setters,但测试类调用user.getPassword()时出现以下编译错误:
java: cannot find symbol symbol: method getPassword() location: variable user of type com.joaofilippe.authorizationmicroservice.app.domain.entities.User
相关代码
User类
package com.joaofilippe.authorizationmicroservice.app.domain.entities; import lombok.*; import java.util.UUID; @Data @AllArgsConstructor @NoArgsConstructor public class User { private UUID id; private String username; private String password; private String email; public User(@NonNull String username, @NonNull String password, @NonNull String email) { this.id = UUID.randomUUID(); this.username = username; this.password = password; this.email = email; } }
CreateUseCase类
package com.joaofilippe.authorizationmicroservice.app.domain.usecases.user; import com.joaofilippe.authorizationmicroservice.app.domain.entities.User; import com.joaofilippe.authorizationmicroservice.app.domain.repositories.IUseRepository; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; public class CreateUseCase extends UserUseCase { public CreateUseCase(IUseRepository userRepository) { super(userRepository); } public User execute(User user) { if (user == null) { throw new IllegalArgumentException("User cannot be null"); } if (user.getPassword() == null || user.getPassword().isEmpty()) { throw new IllegalArgumentException("Password cannot be null or empty"); } user.setPassword(this.hashPassword(user.getPassword())); return super.userRepository.save(user); } private String hashPassword(String password) { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); return passwordEncoder.encode(password); } }
CreateUseCaseTest类
package com.joaofilippe.authorizationmicroservice.app.domain.usecases.user; import com.joaofilippe.authorizationmicroservice.app.domain.entities.User; import com.joaofilippe.authorizationmicroservice.app.domain.repositories.IUseRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import static org.mockito.Mockito.verify; public class CreateUseCaseTest { private IUseRepository userRepository; private CreateUseCase createUseCase; @BeforeEach void setUp() { userRepository = Mockito.mock(IUseRepository.class); createUseCase = new CreateUseCase(userRepository); } @Test @DisplayName("Should save user when user is valid") public void execute_ShouldSaveUser_WhenUserIsValid() { User user = new User( "username", "password", "email"); createUseCase.execute(user); verify(userRepository).save(user); } @Test @DisplayName("Should hash the password") public void execute_ShouldHashPassword() { User user = new User(); user.setPassword("password"); User createdUser = createUseCase.execute(user); verify(userRepository).save(user); assert createdUser.getPassword() != null; assert user.getPassword() != createdUser.getPassword(); } }
pom.xml相关配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.joaofilippe</groupId> <artifactId>authorizationMicroservice</artifactId> <version>0.0.1-SNAPSHOT</version> <name>authorizationMicroservice</name> <description>authorizationMicroservice</description> [...] <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.18.20.0</version> <scope>provided</scope> </dependency> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </plugin> <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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <goals> <goal>properties</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.5.2</version> <configuration> <argLine>@{argLine} -javaagent:${settings.localRepository}/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar </argLine> <argLine>@{argLine} -Xshare:off</argLine> </configuration> </plugin> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.18.20.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
已尝试的操作
- 更新pom.xml配置
- 重装IntelliJ Lombok插件
- 添加相关插件配置
问题仍未解决。
解决方案
1. 修正Lombok核心依赖配置
当前pom.xml中引入的是lombok-maven-plugin依赖,这是插件而非核心依赖,需要替换为Lombok核心库:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> <version>1.18.34</version> <!-- 选择与Spring Boot 3.4.1兼容的版本,建议使用最新稳定版 --> </dependency>
2. 统一注解处理器版本
确保maven-compiler-plugin的annotationProcessorPaths中指定的Lombok版本与依赖版本一致,避免版本不匹配:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.34</version> <!-- 和依赖版本保持一致 --> </path> </annotationProcessorPaths> </configuration> </plugin>
3. 移除多余的Lombok Maven插件
lombok-maven-plugin的delombok目标会将注解转换为实际代码,可能干扰正常的注解处理器工作,若无需delombok功能,建议移除该插件配置:
<!-- 移除以下插件配置 --> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.18.20.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> </plugin>
4. 验证IntelliJ注解处理配置
- 打开
Settings->Build, Execution, Deployment->Compiler->Annotation Processors,确保Enable annotation processing已勾选 - 打开
Settings->Plugins,确认Lombok插件已启用并更新至最新版本
5. 清理缓存并重新构建
- 执行Maven命令:
mvn clean install - 在IntelliJ中点击
Build->Rebuild Project,清除编译缓存
6. 检查测试编译配置
确保测试阶段的编译能继承主编译的注解处理器配置,无需额外修改maven-surefire-plugin,只要主编译配置正确,测试编译会自动使用相同的注解处理器。
内容的提问来源于stack exchange,提问作者João Filippe Rossi Rodrigues
相关产品推荐
相关产品推荐

