腾讯云
开发者社区
文档
建议反馈
控制台
登录/注册
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
搜索
搜索
关闭
发布
首页
标签
junit5
#
junit5
关注
专栏文章
(14)
技术视频
(0)
互动问答
(1)
使用springboot2.5.3运行junit5测试报错?
1
回答
java
、
spring boot
、
classnotfoundexception
、
junit
、
junit5
猫大人
Apache ShenYu 创始人,Apache Member,Dromara 开源组织创始人,著有《深入理解分布式事务:原理与实战》
1. 确认 spring-boot-starter-test 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> 2. 如果依赖传递有问题,可尝试显式添加spring-test: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> <!-- Spring Boot 2.5.3 对应的版本 --> <scope>test</scope> </dependency> 3. 测试类应使用JUnit 5的@ExtendWith注解,而非旧的@RunWith: import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) // 正确使用 JUnit 5 扩展 @SpringBootTest public class MyTest { // 测试方法 } 4. 如果还有问题运行以下命令检查依赖树: mvn dependency:tree -Dincludes=org.springframework:spring-test,org.junit.jupiter 5. 清理并重新构建项目 清理Maven本地仓库(~/.m2/repository)中残留的无效依赖。 在IDE中执行以下操作: Maven:mvn clean install -U IntelliJ/Eclipse:刷新依赖并重启IDE。...
展开详请
赞
2
收藏
0
评论
0
分享
1. 确认 spring-boot-starter-test 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> 2. 如果依赖传递有问题,可尝试显式添加spring-test: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> <!-- Spring Boot 2.5.3 对应的版本 --> <scope>test</scope> </dependency> 3. 测试类应使用JUnit 5的@ExtendWith注解,而非旧的@RunWith: import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) // 正确使用 JUnit 5 扩展 @SpringBootTest public class MyTest { // 测试方法 } 4. 如果还有问题运行以下命令检查依赖树: mvn dependency:tree -Dincludes=org.springframework:spring-test,org.junit.jupiter 5. 清理并重新构建项目 清理Maven本地仓库(~/.m2/repository)中残留的无效依赖。 在IDE中执行以下操作: Maven:mvn clean install -U IntelliJ/Eclipse:刷新依赖并重启IDE。
热门
专栏
丑胖侠
716 文章
78 订阅
程序猿DD
1.9K 文章
85 订阅
开源优测
532 文章
113 订阅
Java开发必知必会
122 文章
33 订阅
啄木鸟软件测试
850 文章
42 订阅
领券