@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就可以完成大部分的单元测试.但是在集成测试方面就比较吃力了.单元测试层面你 ...
随机推荐
- 02: DOM 实例
1.1 Event 对象 <body> <a id="myAnchor" href="http://www.microsoft.com"> ...
- 安装使用composer基本流程
composer工作原理: 这里经过几个步骤:1.composer读取composer.json(这个文件手动建立,官网有格式),这个json是在当前执行composer目录的,如果目录下没有这个js ...
- Ubuntu18.04安装Openssl-1.1.1
1.查看版本 Ubuntu的版本是18.04.使用openssl version命令查看openssl版本,可以看到Ubuntu自带了openssl-1.1.0版本,因此安装新版本需要替换旧版本. 2 ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 3 章 答案
判断对错 1.由计算机存储和操作的信息称为数据.2.由于浮点数是非常准确的,所以通常应该使用它们,而不是int.3.像加法和减法这样的操作在mAth库中定义.4.n 项的可能排列的数目等于 n!.5. ...
- Oracle RAC(Real Application Clusters)とは
ここからはOracle RAC(Real Application Clusters)の基本的な概念について述べていきたいと思います. 「RAC」の読み方は普通に「ラック」です. その前に.通常のデータ ...
- POJ 1270 Following Orders(拓扑排序)题解
Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...
- mysql 通过binlog 查看执行日志
环境:linux deepin15.7 mysql 5.7 1.开启binlog vim /etc/mysql/mysql.conf.d/mysqld.cnf 添加 log_bin = /var/ ...
- ubuntu 安转redis
一 ,redis 安装配置 在 Ubuntu 系统安装 Redis 可以使用以下命令: sudo apt-get update sudo apt-get install redis-server 这样 ...
- 利用Ajax和Servlet实现输入框提示功能
目的和效果: 输入框输入字符串x,匹配后台传入的数据str,如果str中的元素包含字符串x,则提示. 后台代码: String x = request.ge ...
- 【TCP/IP详解 卷一:协议】第十一章 UDP 用户数据报协议
11.1 引言 UDP 是一个简单的 面向数据报 的运输层协议:进程的每个 输出操作 都正好产生一个 UDP数据报,并且组装成一份待发送的IP数据报. 这与 TCP 不一样,它是 面向流字符 的协议, ...