@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就可以完成大部分的单元测试.但是在集成测试方面就比较吃力了.单元测试层面你 ...
随机推荐
- c++性能之对象与指针性能比较、以及java与c++性能对比实测
为了更加直观的比较,好吧,我们选择以对象的初始化并add到list为例子. 首先,定义object如下: #include <string> #pragma once using name ...
- AP聚类算法
一.算法简介 Affinity Propagation聚类算法简称AP,是一个在07年发表在Science上的聚类算法.它实际属于message-passing algorithms的一种.算法的基本 ...
- assert函数用法总结【转】
本文转载自:http://blog.csdn.net/u014082714/article/details/45190505 assert宏的原型定义在<assert.h>中,其作用是如果 ...
- 批量启动application pool
在powershell中执行 Get-ChildItem IIS:\AppPools | where {$_.state -eq "Stopped"} | Start-WebApp ...
- NS3 实验脚本的编写步骤
第一步:配置主机,安装模块 (1)创建N个节点: NodeContainer nodes; nodes.Creat(N); 比如我目前接触到的PointToPoint,N就是2 (2)利用拓扑助手He ...
- NOI 4977 怪盗基德的滑翔翼(LIS)
http://noi.openjudge.cn/ch0206/4977/ 描述: 怪盗基德是一个充满传奇色彩的怪盗,专门以珠宝为目标的超级盗窃犯.而他最为突出的地方,就是他每次都能逃脱中村警部的重重围 ...
- pandas demo 示例
#构造 import pandas as pd import pickle import numpy as np dates=pd.date_range() df = pd.DataFrame(np. ...
- Qt5_加载DLL
1.QLibrary 加载普通 DLL //写清楚库的路径,如果放在当前工程的目录下,路径为./Oracle.so QLibrary *libOCI = new QLibrary("F:\\ ...
- c++ primer plus 第四章 课后题答案
#include<iostream> #include<string> using namespace std; int main() { string first_name; ...
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...