@RunWith和 SpringJUnit4ClassRunner ---->junit4和Spring一起使用
今天在看Spring的Demo的时候,看到了如此单元测试的写法
如下:
@RunWIth(SpringJunit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}
public class MyTest
{
@Test
public void hehe()
{
//.......
}
}
这种写法是为了让测试在Spring容器环境下执行。
Spring的容器环境是啥呢?
比如常见的 Service Dao Action , 这些个东西,都在Spring容器里,junit需要将他们拿到,并且使用来测试。
好,笔者写一个十分简单的demo让大家有个体会!
显示demo的项目结构

要写的东西就两个: applicationContext.xml 和MyTest.java
applicationContext.xml 中仅仅只定义了一个Date对象。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="date" class="java.util.Date"/> </beans>

接下来是MyTest.java的内容
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource;
import java.util.Date; /**
* Created by HuLuo on 2016/8/19.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyTest
{
@Resource
Date date; @Test
public void hehe()
{
System.out.println(date.toLocaleString());
}
}

最后只需要运行就可以了。

最后如图所示,成功拿到了Spring容器里的Date对象。
诸如哪些 Action Service Dao ServiceImpl DaoImpl都是一个道理,可以通过这种方式拿到,然后进行单元测试。。。
@RunWith和 SpringJUnit4ClassRunner ---->junit4和Spring一起使用的更多相关文章
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
- 使用JUnit4测试Spring
测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...
- Junit4测试Spring
使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境 @ContextCon ...
- 当使用junit4 对spring框架中controller/service/mapper各层进行测试时,需要添加的配置
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {&quo ...
- junit4测试 Spring MVC注解方式
本人使用的为junit4进行测试 spring-servlet.xml中使用的为注解扫描的方式 <?xml version="1.0" encoding="UTF- ...
- java 与大数据学习较好的网站
C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...
- Spring+JUnit4单元测试入门
(一).JUnit介绍 JUnit是Java中最有名的单元测试框架,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. Maven导入juni ...
- 使用 Spring 进行单元测试
一.使用spring中对Junit框架的整合功能 除了junit4和spring的jar包,还需要spring-test.jar.引入如下依赖: <dependency> <grou ...
- Spring-Context之二:使用Spring提供的测试框架进行测试
Spring框架是无侵入性的,所以你的代码可以完全是POJO(plain old java object),直接使用Junit就可以完成大部分的单元测试.但是在集成测试方面就比较吃力了.单元测试层面你 ...
随机推荐
- 安装webpack出现警告: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
警告如下: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_mo ...
- 20165310java_blog_week6
2165310 <Java程序设计>第6周学习总结 教材学习内容总结 String 构造 String str=new String() String (char a[]) String ...
- bzoj 1010 玩具装箱toy -斜率优化
P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- C语言: 两个int变量相除,结果保留两位小数
#include<stdio.h> void main() { ,j=; float h; h=(*/)/; printf("%.2f",h); } 注:%f:不指定宽 ...
- ubuntu下git clone 提速
环境:ubuntu16.04 方法:通过socks5代理并且使用http链接 步骤: 1.设置全局使用socks5代理,并且使用http传输 git config --global http.prox ...
- java 插件安装
Emmet插件 : https://www.cnblogs.com/lxjshuju/p/7136420.html 使用方法: 在JSP中使用快捷键 ctrl+e 同其他文件的TAB键
- BZOJ 1001: [BeiJing2006]狼抓兔子(s-t平面图+最短路求最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 题意: 思路:这道题目是最小割题目,但是吧你直接套用Dinic是会超时的. 这里有种很奇妙的做 ...
- WPF基础学习笔记整理 (八) 命令
基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...
- ros 启动launch文件,附带参数
roslaunch cartographer_ros cartographer_ref.launch resolution:=0.07 #下面是cartographer_ref.launch的内容 & ...