Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved
来源:互联网 发布:数据管理员面试题 编辑:程序博客网 时间:2024/05/23 10:33
spring框架项目用junit做测试时,程序在出现下问题:
程序的问题是项目中找不到org.junit.runners.BlockJUnit4ClassRunner,有两种可能,一是没有导入junit的jar, 二是导入的junit的jar的版本中没有此类。
总结:
1、如果导入过jar,就充分可以肯定是版本问题,junit换一个高一些的版本,问题解决。
2、经过调查了解到Spring3提供了SpringJUnit4ClassRunner基类让我们可以很方便的接入JUnit4,而org.springframework.test.context.junit4.SpringJUnit4ClassRunner 继承了org.junit.runners.BlockJUnit4ClassRunner,这里BlockJUnit4ClassRunner类是在junit4.5以上的版本才含有的。
3、使用junit4.11版本时,官网里是要求junit-4.11.jar + hamcrest-core-1.3.jar,如果不加hamcrest-core-1.3.jar会报Java.lang.NoClassDefFoundError:
org/hamcrest/SelfDescribing错误。如果用4.11版本需要两个Jar都加上,或者换成低一点的版本.
4、另有一个会引起此问题的原因是:项目中没有加入Junit单元测试模块;
解决:右键工程->Build Path->Libraries->Add Library..->Junit,将Junit添加进入工程即可
转自:http://blog.csdn.net/houfeng30920/article/details/51373169
感谢!
附上自己的Spring中使用Junit单元测试示例代码
package com.test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @ClassName: JunitTest* @Description: 测试用主类,其他需要注入Spring容器的测试类均可继承此类* @author CHB* @date 2017年9月30日 上午11:03:01* */@RunWith(value = SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:****/****/****.xml"})public class JunitTest {}
package com.test;import org.junit.Test;import org.junit.Ignore;import org.springframework.beans.factory.annotation.Autowired;/*** @ClassName: LedaJunitTest* @Description: * 测试方法,继承JunitTest.java;* 常用注解:@Transactional //标明此方法需使用事务* 常用注解:@Rollback(false) //标明使用完此方法后事务不回滚,true时为回滚,* 一般在测试操作数据库时使用,防止数据库数据混乱* @author CHB* @date 2017年9月30日 上午11:04:49*/public class LedaJunitTest extends JunitTest{ @Autowired //注入的bean @Test public void test(){ //具体测试方法 } @Test @Ignore //执行时,忽略此测试方法 public void test_1(){ //具体测试方法 }}
- Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved
- Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved
- The type org.junit.runners.BlockJUnit4ClassRunner cannot be resolved. It is indirectly referenced fr
- org.junit.internal.runners.JUnit4ClassRunner
- The import junit cannot be resolved解决问题
- 在hibernate和spring项目用junit测试总是出现java.lang.Boolean cannot be cast to java.lang.Integer
- NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
- NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
- java.lang.Error: Unresolved compilation problem: The import org.junit cannot be resolved
- run junit :java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
- SSH2 JUNIT Test Method-ActionContext cannot be resolved(未解决)
- 关于 “The import junit cannot be resolved”的提示错误
- Spring中,在继承JdbcDaoSupport时,出现The type org.springframework.dao.support.DaoSupport cannot be resolved.
- spring中出现的错误:The import org.springframework cannot be resolved
- maven下的junit测试报错处理java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
- spring错误 org.springframework.dao.DataAccessException cannot be resolved
- 【JUnit】@Test 报错,"Test cannot be resolved to a type"
- Class<SpringJUnit4ClassRunner> cannot be resolved to a type spring4.32的junit测试报错解决
- imooc-C++远征之封装篇(上)
- Spring-boot中读取核心配置文件application和自定义properties配置文件的方式
- PHP实现多图片上传
- .net中使用outlook发送邮件
- 论文阅读-Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields
- Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved
- 如何用AndroidStudio将代码上传至Maven仓库
- 第二周项目三-体验复杂度
- 有关Lucene的问题(8):用Lucene构建实时索引的文档更新问题
- 深度学习中的验证集和超参数简介
- 使用 CAS 在 Tomcat 中实现单点登录
- Mybatis多对多转化为一对多查询,简单易学(SSM框架)
- 匈牙利算法
- mysql性能优化-慢查询分析、优化索引和配置