今天在看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一起使用的更多相关文章

  1. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  2. 使用JUnit4测试Spring

    测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...

  3. Junit4测试Spring

    使用Junit4.4测试 在类上的配置Annotation  @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境  @ContextCon ...

  4. 当使用junit4 对spring框架中controller/service/mapper各层进行测试时,需要添加的配置

    @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {&quo ...

  5. junit4测试 Spring MVC注解方式

    本人使用的为junit4进行测试 spring-servlet.xml中使用的为注解扫描的方式 <?xml version="1.0" encoding="UTF- ...

  6. java 与大数据学习较好的网站

    C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...

  7. Spring+JUnit4单元测试入门

    (一).JUnit介绍 JUnit是Java中最有名的单元测试框架,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. Maven导入juni ...

  8. 使用 Spring 进行单元测试

    一.使用spring中对Junit框架的整合功能 除了junit4和spring的jar包,还需要spring-test.jar.引入如下依赖: <dependency> <grou ...

  9. Spring-Context之二:使用Spring提供的测试框架进行测试

    Spring框架是无侵入性的,所以你的代码可以完全是POJO(plain old java object),直接使用Junit就可以完成大部分的单元测试.但是在集成测试方面就比较吃力了.单元测试层面你 ...

随机推荐

  1. 计算两个集合的差集——第六期 Power8 算法挑战赛

    第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...

  2. IDEA 插件-码云

    插件安装 最新插件版本: 2018.3.1.(2019-01-10 发布)注意:码云 IDEA 插件已由 gitosc 更名为 gitee.新版插件 gitee 菜单已经和 git 菜单合并 通过「插 ...

  3. ssh服务的最佳实践

    工作中ssh的最佳实践: 不要使用默认端口 禁止使用protocol version 1 (默认centos6/7已经禁止使用第一版了,但是centos5可能还有在用第一版本) 限制可登陆用户 设定空 ...

  4. C#面试题集锦

    1.貌似最常用的是  值类型和引用类型,还有 装箱和 http://www.cnblogs.com/Autumoon/archive/2008/08/18/1270685.html http://ww ...

  5. 基础dp 记录

    51nod 1134 最长递增子序列 #include<iostream> #include<cstdio> #include<cstring> #include& ...

  6. HTTP 随笔

    浏览器发送HTTP请求主要分为三部分请求行,Response Headers(响应头信息)和Request Headers(请求头信息). 请求行有分为三部分:请求方法,请求路径和请求协议 请求方法有 ...

  7. python 中的object与type的关系

    object 和 type的关系很像鸡和蛋的关系,先有object还是先有type没法说,obejct和type是共生的关系,必须同时出现的. 在看下去之前,也要请先明白,在Python里面,所有的东 ...

  8. Codeforces 766C - Mahmoud and a Message

    C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Python map/reduce

    2017-07-31 18:20:59 一.map函数 map():会根据提供的函数对指定序列做映射.第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 ...

  10. 只输FLOAT值 TEXTBOX

    if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (in ...