Spring Junit集成测试
例子如下:
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集成测试的更多相关文章
- spring junit 做单元测试,报 Failed to load ApplicationContext 错误
spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...
- spring+xml集成测试(准备数据和验证项的外部文件化)
Spring的集成测试 单位测试和集成测试,我想大家都做过,一般情况下,一般逻辑且不需要操作数据库的情况比较适合于单位测试了.而对于一个数据库应用来说,集成测试可能比单元测试更重要,你可以想象,一个互 ...
- spring + junit 测试
spring + junit 测试 需要一个工具类 package com.meizu.fastdfsweb; import org.junit.runner.RunWith; import org. ...
- JUnit + Spring + Hibernate 集成测试,无法通过的问题
使用JUnit测试DAO层.由于不能破坏数据现场,故所有的测试类都继承了Spring测试框架下的 org.springframework.test.AbstractTransactionalDataS ...
- Junit集成测试
Spring4.x高级话题(七):Spring的测试 一. 点睛 测试是开发工作中不可缺少的部分,单元测试只针对当前开发的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖:但是仅仅单元 ...
- spring+junit单元测试
<1>读取文件: 配置文件在classes下:locations = {"classpath*:/spring/applicationContext.xml"} 配置文 ...
- spring junit
转载自 http://blog.csdn.net/funi16/article/details/8691575 在写单元测试的时候,一般是对数据库进行增删改查的操作,这个时候,如果之前删除了某条记录, ...
- Spring Junit 读取WEB-INF下的配置文件
假设Spring配置文件为applicationContext.xml 一.Spring配置文件在类路径下面 在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面 ...
- maven+spring+junit测试要注意的事情
使用maven方式创建webapp工程的资料网上一大堆,在这里也不详细说了.在创建完成之后,里面说到要转动态web工程时要切换为3.0版本,但是我本地切换不了,网上的方法好像也没用,暂时也没用到这块. ...
随机推荐
- 20172325『Java程序设计』课程 结对编程练习_四则运算第三周阶段总结
20172325『Java程序设计』课程 结对编程练习_四则运算第三周阶段总结 结对伙伴 学号:20172306 姓名:刘辰 在这次项目的完成过程中刘辰同学付出了很多,在代码的实践上完成的很出色,在技 ...
- HTML的报告
import HTMLTestRunner class HTMLReporter(object): def reporter(self,filename,reportername,reporterdi ...
- magento中根据用户的id获取用户的所有订单以及每个订单中的物品 以及物品的相关属性
本篇文章是对于已经有了magento基础的人而言,在某个模块的额controller中写任意一个函数. public function goodbyeAction() { for ($customer ...
- Java界面编程—事件监听机制
组件首先要先注册事件处理器,当用户单击组件.移动鼠标或者敲击键盘时都会产生事件(Event),一旦有时间发生,应用程序就会做出对该事件的响应,这些组件就是事件源(Event source). 接受.解 ...
- html中 和空格的区别
Non-Breaking Space 注意是 这6个字符是一个整体, 在html中, 是空格的占位符.一个 代表一个空格:两个 代表两个空格,即使用几个 就显示几个空格. 但是普通的空格在h ...
- MySQL5.7的安装(CentOS 7 & Ubuntu 16.04)
CentOS 通过 yum 安装MySQL5.7 Yum Repository 下载地址:https://dev.mysql.com/downloads/repo/yum/ 选择相应的版本进行下载:R ...
- Spring 系列教程之默认标签的解析
Spring 系列教程之默认标签的解析 之前提到过 Spring 中的标签包括默认标签和自定义标签两种,而两种标签的用法以及解析方式存在着很大的不同,本章节重点带领读者详细分析默认标签的解析过程. 默 ...
- 把dataset对象转换成list集合方法
public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...
- 微信小程序获取当前位置
详细参数说明请看小程序api文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openLocation.html wx.getLoc ...
- ros主从关系
主机: 在~/.bashrc里面输入 export ROS_MASTER_URI=http://localhost:11311export ROS_HOSTNAME=192.168.4.1 其ip地址 ...