Spring测试异常:单独运行通过但Maven构建时测试被跳过
问题描述
单独运行ResourceControllerTests测试类时所有用例均可通过,但执行mvn test命令构建项目时,测试用例被跳过,出现如下错误日志:
2023-05-03T08:10:06.508+01:00 INFO 57176 --- [ main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.schrodingdong.resourcemanager.controller.Tests]: Tests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. Test ignored. Test ignored. Test ignored. Test ignored. java.lang.NullPointerException: Cannot invoke "java.nio.file.attribute.BasicFileAttributes.lastModifiedTime()" because "this.attrs" is null
相关代码
ResourceController类
@RestController @RequestMapping("/resource") @CrossOrigin public class ResourceController { private final ResourceService resourceService; public ResourceController(ResourceService resourceService) { this.resourceService = resourceService; } @PostMapping("/upload") public ResponseEntity<?> uploadObject(@RequestParam ("file") MultipartFile multipartFile, @RequestParam String userMail, @RequestParam String resourceDescription, @RequestParam String resourceVisibility) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { // ... } @GetMapping("/download") public ResponseEntity<?> downloadObject(@RequestParam String resourceId, @RequestParam String userMail, @RequestParam String downloadName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { //... } @NotNull private String getExtension(String fileName) { // ... } @DeleteMapping("/delete") public ResponseEntity<?> deleteObject(@RequestParam String resourceId, @RequestParam String userMail) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { // ... } }
ResourceControllerTests测试类
@WebMvcTest(ResourceController.class) public class ResourceControllerTests { @Autowired private MockMvc mockMvc; @MockBean private ResourceService mockResourceService; private ObjectMapper objectMapper; @BeforeEach void setUp() { objectMapper = new ObjectMapper(); } @Test void uploadObject() throws Exception { // ... } @Test void downloadObject() throws Exception { // ... } @Test void deleteObject() throws Exception { // ... } }
注:项目中另一控制器类无论单独运行还是通过Maven测试均正常。
解决方案
处理无效测试类
Tests:错误日志明确指向com.schrodingdong.resourcemanager.controller.Tests类,该类未正确配置Spring测试上下文。检查该类:- 若为误创建的空测试类,直接删除;
- 若为有效测试类,补充
@WebMvcTest或@SpringBootTest等必要注解,确保测试上下文配置正确。
修复文件操作NPE:针对空指针异常,检查文件属性相关逻辑:
- 在生产代码(如
uploadObject、downloadObject方法)中,添加BasicFileAttributes的null校验,避免调用lastModifiedTime()时出错; - 测试用例中若模拟了文件操作,确保文件属性相关模拟逻辑已正确初始化,避免返回null。
- 在生产代码(如
清理缓存重新测试:执行
mvn clean test,清理Maven缓存和编译产物后重新运行测试,排除缓存导致的异常。校验依赖版本:确保
spring-boot-starter-test版本与项目Spring Boot版本一致,避免依赖冲突引发测试上下文加载问题。
内容的提问来源于stack exchange,提问作者AHamza
相关产品推荐
相关产品推荐

