转载自 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. Android Studio Lint 自动检查清除冗余资源

    (Android Lint) 辛苦的花了很长的周期 做完了项目. 但是打包完发现 APK 的大小让你瞠目结舌 是不是甚至连自己不知道哪里拷贝过来了代码 和 代码相关的布局文件 资源等, 哪些被使用 哪 ...

  2. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. css实现阴影效果(box-shadow)

    box-shadow 使用方法 设置块阴影 语法: box-shadow:<length> <length> <length> <length> ||  ...

  4. WPF 中保存 window(窗口)或者canvas成图片

    最近需要用到这个功能,搜了一下不少代码有问题 ,找到一个效果比较好的,支持多级子元素 记一下. private void button_save_window_Click(object sender, ...

  5. IE中Keep-Alive机制引起的错误

    我们知道Http协议是基于TCP/IP连接的,也就是说客户端浏览器向服务器发出一个Http请求并得到响应是要建立一条TCP/IP连接的,但是如果每发出一个Http请求客户端就要向服务器端建立一条TCP ...

  6. python 之post、get与cookie实战

    项目名称:登陆考勤管理系统爬取个人考勤信息并写入excel表格 编写目的: 公司经常要统计员工的考勤信息,而员工每次都要登陆考勤系统,再复制相关信息出来,贴到EXCEL,再转给统计人员,统计人员再挨个 ...

  7. 初识WebSocket协议

    1.什么是WebSocket协议 RFC6455文档的表述如下: The WebSocket Protocol enables two-way communication between a clie ...

  8. TextView与Html相结合的具体方法?

    TextView与Html相结合的具体方法? android教程之textview解析带图片的html示例 Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大 ...

  9. 文件_ _android从资源文件中读取文件流并显示的方法

    ======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...

  10. USB编程研究之二(常见设备类型的GUID)

    在USB编程之前要事先了解一下GUID的概念. 应用其他网页中的定义: 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体 ...