转载自 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. map的应用

    1.map最基本的构造函数:    map<string , int >mapstring;         map<int ,string >mapint;    map&l ...

  2. 读取 RSSI

    在 TI 给的 SimpleBleCentral demo 里,读取 RSSI 的原理是:按 CC2540EM 的下键,然后调用 GapCentralRole 里的函数,启动定时器,不断向 OSAL ...

  3. 使用 .bash_profile与.bashrc修改字符集

    发现终端设置为UTF8显示以后 svn打印终端就一直乱码, 是用户字符集的原因 有人建议 修改.bashrc 有人建议修改~/.bash_profile 搜索了下区别 /etc/profile:此文件 ...

  4. 函数调用方式__stdcall、__cdel

    函数调用方式关系对比如下: 关键字 调用规则 参数传递方向 返回 参数寄存器 堆栈的清除 __cdecl C语言 从右向左 EAX 无 调用者 __stdcall Win32标准  从右向左 EAX ...

  5. LeetCode "Binary Tree Level Order Traversal II" using DFS

    BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...

  6. docker跨容器之使用link大法通信

    容器1 docker run --name elixir -it edib/elixir-phoenix-dev /bin/bash ip address看看自己的ip 容器2 docker run ...

  7. myeclipse的debug模式中breakpoint窗口怎么调出来

    myeclipse的debug模式中breakpoint窗口怎么调出来? 解决办法: window-->show view-->breakpoints.   如下:

  8. 用Visual Studio 2015 编写 MASM 汇编程序(一)环境配置

    原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject 下面内容根据上面文章翻译整理而来! 开发32位 ...

  9. NPOI生成单元格(列)下拉框

    客户提出能否将导入模板中,课程一列添加下拉框方便选择,不用手输入,以减少输入错误的可能性.于是在网上找了点代码,稍加整理后,形成了以下方案,代码部分: 一:生成课程列表,并放置在excel的单独she ...

  10. X-Cart 学习笔记(三)X-Cart框架2

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 3.了解 ...