同样是测试,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测试框架搭建的更多相关文章

  1. 使用 Spring 2.5 TestContext 测试框架

    Spring 2.5 TestContext 测试框架用于测试基于 Spring 的程序,TestContext 测试框架和低版本 Spring 测试框架没有任何关系,是一个全新的基于注解的测试框架, ...

  2. Spring MVC测试框架

    原文链接:http://jinnianshilongnian.iteye.com/blog/2004660 Spring MVC测试框架详解——服务端测试 博客分类: springmvc杂谈 spri ...

  3. Spring MVC测试框架详解——服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  4. (一)springmvc+spring+mybatis+maven框架搭建

    (一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...

  5. Spring MVC + jpa框架搭建,及全面分析

    一,hibernate与jpa的关系 首先明确一点jpa是什么?以前我就搞不清楚jpa和hibernate的关系. 1,JPA(Java Persistence API)是Sun官方提出的Java持久 ...

  6. struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

  7. struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

  8. 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤

    一.整体概览 首先看maven工程的创建 二.各层的文件配置 2.1,SSM父工程 <span style="font-family:KaiTi_GB2312;font-size:18 ...

  9. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

随机推荐

  1. 各类 HTTP 返回状态代码详解

    完整版 1**(信息类):表示接收到请求并且继续处理 100——客户必须继续发出请求 101——客户要求服务器根据请求转换HTTP协议版本 2**(响应成功):表示动作被成功接收.理解和接受 200— ...

  2. SQL Server(五)——常用函数

    1.数学函数:操作一个数据,返回一个结果 --取上限ceiling select code,name,ceiling(price) from car ; --取下限 floor select floo ...

  3. Remote Desktop File Format

    转自:http://engrmosaic.uncc.edu/mosaic-anywhere/remote-desktop-file-format The new Terminal Services c ...

  4. spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org

    刚刚spark mllib,在maven repository网站http://mvnrepository.com/中查询mllib后得到相关库的最新dependence为: <dependen ...

  5. Terminal中输入命令直接打开QtCreator,以及创建其桌面快捷方式

    工业项目设计学习第一步,熟悉开发工具 Qt学习论坛,东西多,但也杂 emouse的博客,以前学习STM32开发环境搭建时也是参考这位博主的 更多详细的步骤在上面都能找到,今天先不写,等明天把硬件设备全 ...

  6. Stanford机器学习笔记-9. 聚类(Clustering)

    9. Clustering Content 9. Clustering 9.1 Supervised Learning and Unsupervised Learning 9.2 K-means al ...

  7. hdu3065 病毒侵袭持续中

    题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...

  8. Quantum Bogo sort浅谈

    1.普通的猴子排序(bogo sort) 猴子排序百科 en.wikipedia.org/wiki/Bogosort 不停的随机打乱序列,然后检查,直到排好序 复杂度O(n*n!) while not ...

  9. MYSQL基础知识总结

    !注释方式 #    --    单行 /*     */  多行 1.SELECT  column1,column2,column3  FROM tablename WHERE id <= 5 ...

  10. lgy -oracle

    PL/SQL Developer 和 instantclient客户端安装配置(图文) 一: PL/SQL Developer 安装 下载安装文件安装,我这里的版本号是PLSQL7.1.4.1391, ...