Spring Boot 进行测试提示 TestContextAnnotationUtils 错误


在 Spring Boot 运行测试的时候提示错误:

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils 

spring-boot-test-01

问题和解决办法

出现上面的问题是因为我们将 Spring Boot 从版本 2.3.3 升级到了 2.4

在依赖 spring-test 中,我们还是定义 5.2.8.RELEASE 了这个版本。

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.8.RELEASE</version> <scope>test</scope> </dependency>

如果升级版本到 Spring Boot 2.4 的版本的话,你可以首先取消掉 5.2.8.RELEASE 这个版本的限制。

如果取消掉版本的话,你可以看到 Spring 的测试使用的是 5.3.1 的版本。

spring-boot-test-02

上面的错误信息就是因为版本的不同,如果你升级了 Spring Boot 的话,相应的测试版本也需要升级。

当取消掉版本后,再次运行测试。

spring-boot-test-03

通过控制台测试就可以看到版本兼容性问题已经解决没有了。

https://www.ossez.com/t/spring-boot-testcontextannotationutils/13208


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM