解决:The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

近期自己在自家新电脑上搭建了Eclipse + Maven环境,然后在做单元测试的时候,创建Spring测试类,在为测试类添加@RunWith(SpringJUnit4ClassRunner.class)注解的时候,遇到了这个问题:The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved,百度搜索了国内的多个网站,都是Shit的不行,全TM的复制粘贴,十个网站都是同一个问题,同一个答案,同一个标点符号。

根本原因是SpringJUnit4ClassRunner这个类是位于spring-test-4.1.6.RELEASE.jar这个包中的,其他人只谈Junit却根本没提到这一点,所以才导致根本找不到正确的答案。

经过在Stackoverflow上面查找答案,看到了第一个回答之后,瞬间感觉一语中的。原文地址:http://stackoverflow.com/questions/30513375/the-import-org-springframework-test-context-junit4-springjunit4classrunner-canno



6 down vote accepted

You need to add a dependency on spring-boot-starter-test:

<dependency><!--这里是spring-boot的解决办法-->
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
share improve this answer
 
 
Thanks! this solved my problem. –  Juan Carlos  May 28 '15 at 17:49 
 
I had to add also the version but worked also for me –  Aditzu  Sep 21 at 10:45
 
@aditzu If you are using Spring Boot, then adding the version should not be needed. –  Wim Deblauwe  Sep 21 at 13:32
 
这里是使用spring时遇到此问题的解决办法:You are right. For me which I'm not using Spring Boot, a dependency for "spring-test" was the solution. Thanks! – Aditzu Sep 21 at 13:56
注意我标注的绿色的回复,是使用spring而不是使用spring-boot的解决方式,即在自己的pom.xml中添加如下依赖:
 

   <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.1.6.RELEASE</version>
  </dependency>
加上这个依赖之后,等maven把jar包下载下来之后,问题就解决了。

还有人回复说跟junit版本有关的,如果本地的junit版本较低,也请你在pom.xml中增加一下新版本的junit依赖,如下:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

6 down vote accepted

You need to add a dependency on spring-boot-starter-test:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
share improve this answer
 
 
Thanks! this solved my problem. –  Juan Carlos  May 28 '15 at 17:49 
 
I had to add also the version but worked also for me –  Aditzu  Sep 21 at 10:45
 
@aditzu If you are using Spring Boot, then adding the version should not be needed. –  Wim Deblauwe  Sep 21 at 13:32
 
You are right. For me which I'm not using Spring Boot, a dependency for "spring-test" was the solution. Thanks! –  Aditzu  Sep 21 at 13:56
  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 这个错误是由于在使用Spring Boot进行测试时,找不到 spring-boot-test 包中的类 org.springframework.boot.test.context.SpringBootTest。这个问题可能是由于以下几个原因导致的: 1. 缺少Spring Boot Test依赖:检查项目中是否已经添加了Spring Boot Test依赖,如果没有添加则需要手动添加。 2. 版本不一致:检查Spring Boot及Spring Boot Test版本是否与依赖库和引用的其他库的版本匹配。建议在测试和生产环境中使用相同版本的Spring Boot和Spring Boot Test。 3. 依赖冲突:检查依赖库中是否存在与Spring Boot Test冲突的库。如果有,则需要将它们排除在依赖库之外。 4. IDE配置问题:检查IDE是否正确配置了classpath,以便可以正确识别Spring Boot Test类。 解决方法包括: 1. 添加Spring Boot Test依赖:在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 2. 检查版本是否匹配:确保版本匹配,可以通过 Spring Boot 相关文档查看每个版本所支持的库版本。 3. 排除依赖冲突:在 pom.xml 文件中添加以下代码以排除不必要的依赖库: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>com.example</groupId> <artifactId>problematic-dependency</artifactId> </exclusion> </exclusions> </dependency> ``` 4. 配置IDE的classpath:在IDE设置中增加Spring Boot Test依赖:Right-click on the project in the Project Explorer > Properties > Java Build Path > Libraries > Add Library > JUnit > JUnit 4 > Next > Add Library > Maven Managed Dependencies > Next > finish. 如果上述方法都不能解决问题,可以尝试使用其他版本的Spring Boot Test或者修复依赖冲突问题。 ### 回答2: Java编程语言是一种面向对象的语言,被广泛应用于Web开发、移动应用开发、大数据处理以及人工智能等领域。在使用Java进行开发过程中,我们会面临各种各样的错误和异常。其中,一种常见的错误就是“无法访问 org.springframework.boot.test.context.springboottest 错误的类”。 这个错误常出现在使用Spring Boot框架进行测试的时候。Spring Boot框架是一种基于Spring的开箱即用的框架,可以帮助开发者快速搭建Web应用。为了进行测试,开发者通常会使用JUnit和Spring Boot Test框架。在使用Spring Boot Test框架时,如果出现“无法访问 org.springframework.boot.test.context.springboottest 错误的类”的错误,通常是因为引入了不兼容的版本。 解决这个错误的方法有很多种。一种方法是检查Maven或Gradle配置文件中依赖的版本信息是否正确,确保Spring Boot的版本和其它依赖的版本相兼容。另一种方法是从项目中删除无用的依赖,只保留项目所需的依赖,这样可以避免版本冲突和兼容性问题。还可以参考Spring Boot官方文档,查找相关问题的解决方法。 总之,出现“无法访问 org.springframework.boot.test.context.springboottest 错误的类”错误时,我们需要仔细排查问题,找到错误根源,然后采取相应的措施加以解决。只有这样,才能顺利地进行Java开发和测试工作。 ### 回答3: 这个错误通常是因为Spring Boot版本与JUnit版本之间的不兼容导致的。许多Spring Boot项目都使用JUnit测试来确保代码的正确性和可靠性。但是,如果你的版本不匹配,你可能会遇到这个错误。 要解决这个问题,你需要确保Spring Boot和JUnit之间的版本兼容。可以通过在pom.xml文件中添加以下依赖关系来实现这一点: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring.boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> ``` 其中,`${spring.boot.version}`和`${junit.version}`应该被替换为你所使用的Spring Boot和JUnit版本号。 在添加这些依赖关系后,如果你仍然遇到这个问题,你可以尝试升级或降级Spring Boot和JUnit版本,以便它们兼容。或者,你可以考虑使用其他测试框架,如TestNG或Mockito。 此外,你还可以检查是否正确导入了类,是否正确配置了测试环境,以及是否正确使用了注解等。通常,如果你遵循最佳实践和正确配置代码和测试环境,就可以避免这些问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值