java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils问题处理

项目场景:

使用@SpringBootTest进行单元测试。


问题描述

启动直接报错:

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

Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils

原因分析:

这是由于 spring-boot-test 和 spring-test 的版本没有匹配上


解决方案:

对于maven项目:
1、直接不写版本号,让他自动导入

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <scope>test</scope>
</dependency>

2、自己去找匹配的版本号
直接修改依赖的版本号:我这里给一组能够匹配上的

spring-boot-test:2.5.12
spring-test:5.3.19
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.19</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>2.5.12</version>
    <scope>test</scope>
</dependency>

修改后重新加载就行了

解决这个问题的过程:

这里报错没找到对应的类:TestContextAnnotationUtils

Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils

那就肯定有地方用了这个类,直接全局搜索
在这里插入图片描述
发现只有他一个类是红的,但是同一个包里的其他类不是红的,那证明是有这个依赖的
在这里插入图片描述
对于这种只有一个红的,多半就是版本对不上,然后去搜对应的版本就可以了

其他问题:

java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith’s ‘value’ attribute or make the default bootstrapper class available.

原因:spring-web和spring-test版本对不上
解决方案:不写spring-test的版本号

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
</dependency>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值