Spring TestContext测试框架搭建
同样是测试,JUnit和Spring TestContext相比,Spring TestContext优势如下:
1.Spring TestContext可以手动设置测试事务回滚,不破坏数据现场
2.在测试类中不用手动装配bean,只要一个@Autowired即可自动装配
----------------分割线---------------------------
本文记录web project和java project如何使用TestContext测试框架,首先介绍web project
现总结如何使用Spring TestContext测试框架:
1.测试框架环境搭建
要保证你的项目是web project,这样在WebRoot/WEB-INF/lib包中引入spring-test.jar包即可。
(jar包位置:我用的是spring-framework-2.5.5,在这个文件夹中/dist/modules下有这个spring-test.jar)
注意保证你的JUnit版本最好在4.5以上,否则可能会不支持JUnit4.5以下版本。
2.使用TestContext测试框架
applicationContext.xml文件如下,我放在了src下面
<?xml version ="1.0" encoding ="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Annotation声明 -->
<context:annotation-config />
<context:component-scan base-package="com.huohuo" />
<!-- 事务声明 -->
<tx:annotation-driven transaction-manager="txManager"/> <!-- 配置dataSource数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean> <!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.huohuo.domain.User</value>
<value>com.huohuo.domain.Log</value>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 配置事务TXManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
userServiceTest类代码如下:
package com.huohuo.test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional; import com.huohuo.service.UserService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext.xml") //Spring配置文件位置
@TransactionConfiguration(transactionManager="txManager") //我的事务bean 的id为txManager
//重要说明:上面这行如果不加,那么会自动寻找名为transactionManager的事务bean,如果你在applicationContext.xml设置的事务bean不是这个,那么要改成你的事务bean的id名字
@Transactional
public class UserServiceTest {
@Autowired //自动装配bean,或者写成@Resource(name="userService")
private UserService userService; @Rollback(false) //设置事务不回滚,如果不加默认为自动回滚
@Test
public void test() {
userService.saveUserAndLog();
} }
小结
如果您希望在 Spring 环境中进行单元测试,那么可以做如下配置:
- 继续使用 Junit4 测试框架,包括其 @Test 注释标签和相关的类和方法的定义,这些都不用变
- 需要通过 @RunWith(SpringJUnit4ClassRunner.class) 来启动 Spring 对测试类的支持
- 需要通过 @ContextConfiguration 注释标签来指定 Spring 配置文件或者配置类的位置
- 需要通过 @Transactional 来启用自动的事务管理
- 可以使用 @Autowired 自动织入 Spring 的 bean 用来测试
另外您不再需要:
- 手工加载 Spring 的配置文件
- 手工清理数据库的每次变更
- 手工获取 application context 然后获取 bean 实例
-------------分割线-------------
下面介绍普通java类使用TestContext测试框架
1.引入spring-test.jar包(上面有说明,或者去baidu下载)
2.测试类必须继承extends AbstractTransactionalJUnit4SpringContextTests这个类
3.剩下的测试类中使用TestContext和上面web project一样
java project项目中java测试类代码如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext.xml")
@TransactionConfiguration(transactionManager="txManager")
@Transactional
public class UserServiceTest extends AbstractTransactionalJUnit4SpringContextTests { @Rollback(false)
@Test
public void test() {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us =(UserService)ct.getBean("userService");
us.saveUserAndLog();
}
Spring TestContext测试框架搭建的更多相关文章
- 使用 Spring 2.5 TestContext 测试框架
Spring 2.5 TestContext 测试框架用于测试基于 Spring 的程序,TestContext 测试框架和低版本 Spring 测试框架没有任何关系,是一个全新的基于注解的测试框架, ...
- Spring MVC测试框架
原文链接:http://jinnianshilongnian.iteye.com/blog/2004660 Spring MVC测试框架详解——服务端测试 博客分类: springmvc杂谈 spri ...
- Spring MVC测试框架详解——服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- (一)springmvc+spring+mybatis+maven框架搭建
(一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...
- Spring MVC + jpa框架搭建,及全面分析
一,hibernate与jpa的关系 首先明确一点jpa是什么?以前我就搞不清楚jpa和hibernate的关系. 1,JPA(Java Persistence API)是Sun官方提出的Java持久 ...
- struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
一.整体概览 首先看maven工程的创建 二.各层的文件配置 2.1,SSM父工程 <span style="font-family:KaiTi_GB2312;font-size:18 ...
- SSM(Spring +SpringMVC + Mybatis)框架搭建
SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...
随机推荐
- Genome2D编译方法
Genome2D是一个高效的2D引擎,现在支持Flash(stage3d)和HTML5,因为只有作者一个人在维护,就没开源代码. 最近和作者沟通了下,已经开源啦. 作者划分了几个模块,编译起来不是特别 ...
- asp.net MD5 加密
//Md5摘要 string resultMD5 = FormsAuthentication.HashPasswordForStoringInConfigFile("要加密的内容" ...
- 在unix系统下的 .o文件 .a文件 .so文件说明和相互关系
.o文件 .o文件就是对象文件,包含编译好的可执行代码,当程序执行时,被链接库链接调用[相当于windows里的obj文件] .a文件unix中的静态链接库,包含多个需要包含的.o文件,主要特点是在 ...
- PMBOK学习笔记二-项目管理过程
项目管理过程可归纳为五类,即五大项目管理过程组 启动过程组.定义一个新项目或现有项目的一个新阶段,授权开始该项目或阶段的一组过程..规划过程组.明确项目范围,优化目标,为实现目标制定行动方案的一组过程 ...
- 基础篇之 Create Type
Create Type 的话呢,是创建一个自定义的数据类型,等于说为常用的数据类型建造一个别名的样纸.然后就可以通用当前数据库的当前架构.(当然了,一般来说我们都是使用dbo架构,所以都会无事前面那个 ...
- cocoapods降级版本
1.卸载当前版本sudo gem uninstall cocoapods 2.下载旧版本sudo gem install cocoapods -v 0.39.0 PS: 如果之前装有多版本,执行1之后 ...
- Linux IPC System V 信号量
模型 #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> ftok() //获取key ...
- 元组tuple
tuple和list的主要区别就是tuple里的元素的是不能改变的,即tuple时immutable的 #创建tuple >>> tupa = (12 , 'ed' , 34) &g ...
- node js学习(二)——REPL(交互式解释器)
1.简介 Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux shell,我们可以在终端中输 ...
- Java Web之会话技术
客户端与服务器通信过程中,会产生一些数据.比如,A和B分别登陆了某宝购物网站,A买了一个Android手机,B买了一个iPhone手机,当结账时,web服务器需要分别对用户A和B的信息分别保存.根据J ...