例子如下:

package com.junge.demo.spring;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.junge.demo.spring.service.IPerface;
import com.junge.demo.spring.service.ServiceConfig; /**
* Unit test for simple App.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ServiceConfig.class)
public class AppTest {
@Autowired
private List<IPerface> perfaceList; @Test
public void addTest() {
System.out.println("333");
} @Test
public void playTest() {
assertEquals(2, perfaceList.size());
}
}

其中用到了2个注解

1.@RunWith 这是Junit提供的,注意Junit版本要和spring兼容,例如spring用4.1.13.RELEASE版本,Junt可以用4.12版本,如果不兼容,用例跑不起来。

2.@ContextConfiguration 这是spring提供的注解,用来配置spring的上下文配置,如果是使用javaconfig风格配置的,如上面例子所示;如果是使用xml文件配置的,可以用下面的配置:

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })

一般在项目中测试时,会配置一个基类测试类,其他测试类都继承自该基类,这样就不需要在每一个测试类中添加配置。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public abstract class BaseServiceTest { }

Spring Junit集成测试的更多相关文章

  1. spring junit 做单元测试,报 Failed to load ApplicationContext 错误

    spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...

  2. spring+xml集成测试(准备数据和验证项的外部文件化)

    Spring的集成测试 单位测试和集成测试,我想大家都做过,一般情况下,一般逻辑且不需要操作数据库的情况比较适合于单位测试了.而对于一个数据库应用来说,集成测试可能比单元测试更重要,你可以想象,一个互 ...

  3. spring + junit 测试

    spring + junit 测试 需要一个工具类 package com.meizu.fastdfsweb; import org.junit.runner.RunWith; import org. ...

  4. JUnit + Spring + Hibernate 集成测试,无法通过的问题

    使用JUnit测试DAO层.由于不能破坏数据现场,故所有的测试类都继承了Spring测试框架下的 org.springframework.test.AbstractTransactionalDataS ...

  5. Junit集成测试

    Spring4.x高级话题(七):Spring的测试 一. 点睛 测试是开发工作中不可缺少的部分,单元测试只针对当前开发的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖:但是仅仅单元 ...

  6. spring+junit单元测试

    <1>读取文件: 配置文件在classes下:locations = {"classpath*:/spring/applicationContext.xml"} 配置文 ...

  7. spring junit

    转载自 http://blog.csdn.net/funi16/article/details/8691575 在写单元测试的时候,一般是对数据库进行增删改查的操作,这个时候,如果之前删除了某条记录, ...

  8. Spring Junit 读取WEB-INF下的配置文件

    假设Spring配置文件为applicationContext.xml 一.Spring配置文件在类路径下面 在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面 ...

  9. maven+spring+junit测试要注意的事情

    使用maven方式创建webapp工程的资料网上一大堆,在这里也不详细说了.在创建完成之后,里面说到要转动态web工程时要切换为3.0版本,但是我本地切换不了,网上的方法好像也没用,暂时也没用到这块. ...

随机推荐

  1. NC 5的开发环境起不了客户端

    5的开发环境,中间件启动无异常,但是在起客户端时报错,连中间件也关闭了. 添加图中两个jdk下的包

  2. the example of dlsym

    void *handle; int i, (*fptr)(int); /* open the needed object */ handle = dlopen("/usr/home/me/l ...

  3. js使用sessionStorage、cookie保存token

    本文是参考别人的博客写的,图片直接用的别人的 1.Token:token是客户端频繁向服务器端请求数据,服务器频繁的去数据库查询用户名和密码进行对比,判断用户名和密码正确与否,并作出相应的提示,在这样 ...

  4. Eigen中的map

    Map类用于通过C++中普通的连续指针或者数组 (raw C/C++ arrays)来构造Eigen里的Matrix类,这就好比Eigen里的Matrix类的数据和raw C++array 共享了一片 ...

  5. 为什么要使用日志管理?syslog和Windows事件日志

    为什么要使用日志管理?syslog和Windows事件日志 日志管理 - 确保网络安全的先决条件 日志给予您有关网络活动的第一手信息.日志管理确保日志中隐藏的网络活动数据转换为有意义的可操作的安全信息 ...

  6. Element ui 使用 Tree 树形控件

    使用树形控件需要映入 jsx才能运行链接:https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage npm install\ babe ...

  7. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  8. 2018.11.01 NOIP训练 树的排列(树形dp)

    传送门 跟这道题差不多. 只不过是让权值小的儿子做权值大的儿子的父亲而已. 代码

  9. Win7 VS2015环境编译NanoVG

    书接上回,Cairo编译好使用后,发现简单的每帧画100条随机线段就卡得不行,装了个gooreplacer( http://liujiacai.net/gooreplacer/ )上stackover ...

  10. boost-使用format和lexical_cast实现数字和字符串之间的转换

    使用boost的format可以实现数字到string的格式化转换,boost的lexical_cast可以实现string到数值的转换,eg: #include "boost/format ...