`
ray_yui
  • 浏览: 216949 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JUnit结合Spring TestContext使用

阅读更多

前言:本文章需要JUnit单元测试框架的基础知识,若读者还不具备,请阅读笔者的JUnit文章:http://ray-yui.iteye.com/blog/1914106



UnitTest系列文章:
      使用JUnit开发单元测试:http://ray-yui.iteye.com/blog/1914106
      使用DBUnit扩展JUnit:http://ray-yui.iteye.com/blog/1914979
      使用EasyMock扩展Junithttp://ray-yui.iteye.com/blog/1916170
      使用Cactus测试Servlethttp://ray-yui.iteye.com/blog/1917515
      使用Cobertura生成测试覆盖率报告http://ray-yui.iteye.com/blog/1921958




什么是Spring TestContext?
      Spring TestContext是Spring提供的一套基于注解的Test框架,Spring TestContext有非常好的兼容性,可以无缝兼容JUnit,TestNG等单元测试框架,而且在其基础上增加更多的功能


Spring TestContext为我们带来什么?
      在Spring应用大行其道的今天,使用Spring来构建应用已经是再普通不过的事情,但当使用JUnit,TestNG等传统的单元测试技术和Spring进行结合的时候,就会出现很多并不如意的事情
      1.Spring容器初始化问题:此问题已于JUnit4中被解决,通过使用@BeforeClass
         可以有效防止Spring容器被多次初始化的问题

      2.硬编码获取Bean:此问题是由于JUnit并不兼容Spring,所以当单元测试运行
         的时候,无法解释Spring独有的注解,从而需要使用硬编码来获取Bean

      3.数据现场破坏:JUnit当中可以使用DBUnit来进行数据现场维护的解决方案,
         详情可以通过笔者的DBUnit使用了解DBUnit,Spring TestContext通过AOP
         声明式事务来对单元测试进行回滚,有效的解决了数据现场的问题

      4.事务:通常我们的单元测试都需要和数据库进行交互,但传统的JUnit的
        组成单元为TestCase,并不存在事务的概念,而我们大多数情况下都需要
        观察事务的执行过程或总体的性能,特别是对长事务模块的测试,
        Spring TestContext允许单元测试支持事务的控制


Spring TestContext使用:
      首先为Maven增加Spring的依赖,由于Spring有众多依赖,这里就不给出代码了


      以下为Spring TestContext的代码

package com.accentrix.ray;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.ExpectedException;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.annotation.Timed;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;

import com.accentrix.vtc.hpp.entity.planning.User;
import com.accentrix.vtc.hpp.unit.dao.UserDao;

//指定Spring配置文件位置
@ContextConfiguration(locations = "/applicationContext-test.xml")
/*
 *  配置事务是使用哪个事务管理器和默认是否回滚,通常继承
 *  AbstractTransactionalJUnit4SpringContextTests后不需配置
 *  @TransactionConfiguration(defaultRollback = true,transactionManager="test")
 */
public class SpringTest extends AbstractTransactionalJUnit4SpringContextTests{

	// 可以直接使用Spring Annotation的方式注入
	@Autowired
	private UserDao userDao;

	private User user;

	// 当有某些操作不想在包含在事务,希望在事务前执行时,使用@BeforeTransaction
	@BeforeTransaction
	public void beforeTransaction() {
		System.out.println("Hello ");
	}

	/*
	 * 由于继承了AbstractTransactionalJUnit4SpringContextTests,
	 * 此时JUnit就拥有了事务控制,@Before和@After都会包含在事务当中
	 */
	@Before
	public void setUp() {
		System.out.println(userDao);

		// 在开始前先插入测试数据
		user = new User(1, "Ray", "123");
		userDao.add(user);
		/*
		 * 此时使用Spring Test的就需要注意,因为 Hibernate会将HQL或SQL语句进行缓存,所以
		 * 这里若然DAO是使用Hibenrate,需要使用flush
		 */
		userDao.getSession().flush();
	}

	
	@Test
	@ExpectedException(RuntimeException.class) // ①
	@Timed(millis = 1000) // ②
	@Repeat(100) // ③
	@Rollback(false) // ④
	public void testMain() {
		throw new RuntimeException();
	}

	
	@After
	public void tearDown() {
		// 最后删除测试数据
		userDao.delete(user);
	}

	// 当有某些操作不想在包含在事务,希望在事务后执行时,使用@AfterTransaction
	@AfterTransaction
	public void afterTrasaction() {
		System.out.println("World");
	}
}


      请留意上面代码中testMain上的4个注解

@ExpectedException:此形式和@Test(expected=RuntimeException.class)一样,表示该单元测试应该抛出RuntimeException的错误,否者测试不通过,但建议使用JUnit提供的

@Timed:Timed和@Test(timeout=1000)有所不同的,@Test(timeout=1000)代表被注解的方法单体执行超过1秒就会测试失败,但@Timed()为测试整个事务从开始到执行testMain再结束的总耗时,而且会把@Repeat的重复也计算在总耗时内

@Repeat:Repeat代表此方法将会重复执行100次,注意,会重复执行@Before和@After方法

@Rollback:这里是Spring TestContext我认为最大的魅力所在,只需一行简单的注解就可以实现控制回滚操作,此时可以覆盖基类中回滚默认为true的限制,可以控制是否回滚事务, true为回滚,false为不回滚

@NotTransaction:请注意这个注解,在2.5版本此注解为声明该单元测试不需要事务支持,但在3.0版本后认为单元测试当中应该都要有事务控制,所以此注解已被Deprecated(不建议再使用)

      除此以外Spring TestContext还提供了很多方便的操作让我们使用

public void testMain() {
		//获取ApplicationContext对象
		ApplicationContext ac = super.applicationContext;
		
		//获取SimpleJdbcTemplate实现
		SimpleJdbcTemplate  simpleJdbcTemplate= super.simpleJdbcTemplate;
		
		//获取某table的总行数
		super.countRowsInTable("t_user");
		
		//执行某份外部的sql文件,true为当发生错误时仍然继续执行
		super.executeSqlScript("D:/execute.sql", true);
		
		//快速删除某张表的全部记录
		super.deleteFromTables("t_test");
	}


总结:
      Spring TestContext使用极其简单,通过简单的注解就能扩展JUnit单元测试,弥补了JUnit在测试Spring应用时的不足和空白,还可以使用Spring自身的注解对依赖进行注入,简化了此前每次获取Bean的烦恼,在Spring应用中请毫不犹疑的使用Spring Test,在Spring TestContext当中除了支持JUnit4以外,还支持JUnit3.8,TestNG等,只需将单元测试类分别继承AbstractTransactionalJUnit38SpringContextTests和AbstractTransactionalTestNGSpringContextTests即可,
1
2
分享到:
评论
1 楼 小鑫。 2013-08-07  
mark

相关推荐

    spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ).zip

    spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ) spring 5测试支持这个项目作为 5的正式 Prototype,在 spring TestContext框架测试支持,并与 SPR-13575结合到 Spring Framework 。 因此,在...

    使用Spring2.5TestContext测试框架

     概述直接使用JUnit测试Spring程序存在的不足一个需要测试的Spring服务类编写UserService的测试用例准备测试数据并检测运行结果SpringTestContext测试框架体系结构小结参考资料Spring2.5TestContext测试框架用于...

    SpringSpringMvcMybatis

    ################ tips...借助Junit和Spring TestContext framework 和RestController监听器的作用: 监听对象 监听对象的属性 1:统计在线人数和在线用户 HttpSessionListener httpSessionAttributeListener 2:系统启

    junit5-demo:JUnit 5的演示

    此外,还提供了使用Spring框架中的Spring TestContext框架以及Spring Boot中的测试支持的演示。 使用以下技术。 JUnit平台1.5.2 JUnit木星5.5.2 JUnit Vintage 5.5.2 其中包括JUnit 4.12(用于与JUnit Jupiter...

    接口测试白皮书--淘宝(中国)软件有限公司

    2009/8/31 1 接口测试的背景 11 什么是接口测试 ...53 Spring TestContext Framework 54 Unitils 55 TestNG 56 CruiseControl 57 Clover 58 Mock 6 接口测试的方向 7 参考资料 8 作者介绍

    Spring中文帮助文档

    9.5.9. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用TransactionTemplate 9.6.2. 使用PlatformTransactionManager 9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 ...

    Spring API

    9.5.9. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用TransactionTemplate 9.6.2. 使用PlatformTransactionManager 9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

Global site tag (gtag.js) - Google Analytics