转载自 http://blog.csdn.net/funi16/article/details/8691575

在写单元测试的时候,一般是对数据库进行增删改查的操作,这个时候,如果之前删除了某条记录,自然后面的程序就找不到这条记录了,所以可以通过配置spring的事务管理或者测试框架来回滚,减少工作量。使用的数据库是postgreSQL和mysql。

在写这篇文章的时候,很多地方借鉴了下面两篇文章:

http://www.cnblogs.com/rainisic/archive/2012/01/22/Spring_Test_Framework.html

http://blog.163.com/wf_shunqiziran/blog/static/17630720920121084325322/

1、创建测试类

  创建一个测试用的类,推荐名称为 “被测试类名称 + Test”。

  测试类应该继承与 AbstractJUnit4SpringContextTests 或 AbstractTransactionalJUnit4SpringContextTests

  对于 AbstractJUnit4springcontextTests 和 AbstractTransactionalJUnit4SpringContextTests 类的选择:

  如果再你的测试类中,需要用到事务管理(比如要在测试结果出来之后回滚测试内容),就可以使用AbstractTransactionalJUnit4SpringTests类。事务管理的使用方法和正常使用Spring事务管理是一样的。再此需要注意的是,如果想要使用声明式事务管理,即使用AbstractTransactionalJUnitSpringContextTests类,请在applicationContext.xml文件中加入transactionManager bean:

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

  如果没有添加上述bean,将会抛出NoSuchBeanDefinitionException,指明 No bean named 'transactionManager' is definded.

2. 配置测试类

  添加如下内容在class前,用于配置applicationContext.xml文件的位置。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")

3. 创建测试方法

  创建测试用方法,推荐名称为 “被测方法名称+ Test”。

  测试方法上方加入 @Test

4、 通过JUnit 4 执行

  右键方法名,选择则“Run As”→“JUnit Test”即可

我以前也是通过这种方法来配置单元测试的自动回滚的,不过在新的项目里发现不行,于是又是各种度娘谷哥,终于找到了。顺便说下我搜索到的不能自动回滚的集中原因:

1、代码中含有try-catch块导致不能回滚。(个人觉得不靠谱)

2、方法里面有非public方法。

3、加载了2次配置文件,导致insert和rollback方法不在一个事物里面。我遇到的就是这种情况。

如:我在注解当中已经加载了一次applicationContext.xml文件,而在setUp()里面又加载了一次,导致方法被当成2个事物提交了。于是我使用注解来加载bean。

  1. @Autowired
  2. private AppointmentDao appointmentDao;

单元测试用例上面的注解是:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
  3. @Transactional
  4. /**读取配置文件到运行环境。注意:file的路径 */
  5. @ContextConfiguration(locations={"classpath:applicationContext.xml"})

在applicationContext.xml文件当中:

  1. <bean id="txManager"
  2. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource" />
  4. </bean>

spring junit的更多相关文章

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

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

  2. spring + junit 测试

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

  3. spring+junit单元测试

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

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

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

  5. Spring Junit集成测试

    例子如下: package com.junge.demo.spring; import static org.junit.Assert.assertEquals; import java.util.L ...

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

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

  7. JAVA框架 Spring junit整合单元测试

    一.准备工作 1:Junit的需要的jar包: 2.spring的整合的jar包:spring-test-4.2.4.RELEASE.jar 3.代码实现 1) //导入整合的类,帮我们加载对应的配置 ...

  8. Spring+Junit测试用例的使用

    1.[导包]使用Spring测试套件,需要两个jar包:junit-X.X.jar和spring-test-X.X.X.RELEASE.jar,在maven项目下可添加如下依赖: <depend ...

  9. Spring+Junit+Mock测试web项目,即Controller

    准备:Maven依赖 <!-- Spring和MVC的包这里不列出来了,webmvc,aspects,orm,其他maven会自动导 --> <dependency> < ...

随机推荐

  1. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  2. mac osx 快捷键符号以及意义 触发角:锁屏

    快捷键中常用符号⌘(command).⌥(option).⇧(shift).⇪(caps lock).⌃(control).↩(return).⌅(enter). OSX快捷键 快捷键中常用符号 ⌘( ...

  3. 关于MySql的DBHelper类以及数据分页

    前端: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix=& ...

  4. google protocol buffer 使用说明

    一:编译源码 下载地址:http://code.google.com/p/protobuf/downloads/list 下载后,根据编译说明进行编译. windows 平台,直接打开msvc中的工程 ...

  5. linux下的find文件查找命令与grep文件内容查找命令

    在使用linux时,经常需要进行文件查找.其中查找的命令主要有find和grep.两个命令是有区的. 区别:(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访 ...

  6. css3立体旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. edittext_解释

    ============ 2   android判断EditText输入的数字.中文还是字母方法   String txt = edInput.getText().toString(); Patter ...

  8. DDL DML DCL语句

    总体解释:DML(data manipulation language):自动提交的数据库操作语言       它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样 DDL( ...

  9. hihocoder 1391 [扫描线]

    /* 题意: 两方对阵,互发导弹.防护罩可以让导弹原速反向. 每一枚导弹有发射时间航行时间伤害值. 防护罩也有开启时间和防御时间. 其中一方防护罩开启时间已知,求另一方防护罩合理安排开启时间使得己方受 ...

  10. SecureCRT 连接后一些会话选项配置修改