配置测试类
  添加如下内容在class前,用于配置applicationContext.xml文件的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")

***********************
Junit4 不回滚
2012-02-13
 @Rollback
表明被注解方法的事务在完成后是否需要被回滚。 如果true,事务将被回滚,否则事务将被提交。使用@Rollback接口来在类级别覆写配置的默认回滚标志。
@Rollback(false)
public void testProcessWithoutRollback() {
// …
}
@NotTransactional
出现该注解表明测试方法必须不在事务中执行。
@NotTransactional
public void testProcessWithoutTransaction() {
// …
}

*************************************************************
2008-11-24 
在一般的java 项目中,我们对于数据库的操作需要写大量的junit代码,这些测试代码都包含事物。
    在以下情况下:使用插入了一条数据到test database ,每跑一次junit test 都会向test database 里面插入相同的数据。这明显不可取,如果自己写rollback,代码量很大。
那有没有方法实现junit test的回滚,可以使用spring自带的事物管理来解决。
以下为步骤:
1:配置DataSourceTransactionManager
 <!-- config transaction -->
     <bean id="txManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource" />
     </bean>

dataSource为数据库配置:(这里使用了变量配置数据库的用户名和密码)
 <bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close">
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
         <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />         
 </bean>

解析来读取配置文件的配置(注意value里面的路径可以使相对和绝对路径)
  <!--  Reading configuration file -->
     <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
             <list>
                 <value>classpath*:resources/jdbc.properties</value>
             </list>
         </property>
     </bean>
jdbc.properties 的配置如下:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
 jdbc.url=jdbc:oracle:thin:@0.0.0.0:1521:orcl
 jdbc.username=userName
 jdbc.password=password
 jdbc.testOnBorrow=true

以上是spring 数据源的配置。
2:junit class 写法 TestClass.java
/**固定写法 */ 
@RunWith(SpringJUnit4ClassRunner.class)
 /**txManage是配置的事物管理Object  defaultRollback = true 任何情况都回滚*/
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
 /**读取配置文件到运行环境。注意:file的路径 */
@ContextConfiguration(locations = { "file:web/WEB-INF/applicationContext*.xml" })
 @Transactional
 public abstract class TestPersonService {
     /**声明环境里面的bean */            
    @Autowired
    private PersonService personService; 

    @Test
     public void testGetPerson(){
         int userId = 1;
        Person person= personService.getPerson(userId );
         assertNotNull(person);
         assertEquals(userId,person.getUserId());
    }  

}
使用了设置断点的junit写法。下面为非回滚junit class类写法。
private PersonService personService= (PersonService ) OfferMeAppContext.getContext().getBean(
                "personService");

     @Before
     public void setUp() {
         ApplicationContext context = new FileSystemXmlApplicationContext(./web/WEB-INF/applicationContext*.xml)  ;
        this.personService= (PersonService ) context .getContext().getBean(
                "personService");
         dataSource = (DataSource) context .getContext().getBean(
                 "dataSource");
         this.jdbcTemplate = new JdbcTemplate(dataSource);
     }


     
     //@Test
    @Test
    public void testGetPerson(){
        int userId = 1;
        Person person= personService.getPerson(userId );
        assertNotNull(person);
   }
注意:
1:关于配置文件的路径一定要正确
2:spring 配置的object 名字和java类里面写的该类对象的属性名一定要一致
3:spring 的ApplicationContext实现类必须使用ClassPathXmlApplicationContext而不能使用ClassPathResource
ApplicationContext f = new ClassPathXmlApplicationContext("applicationContext.xml");

spring下Junit_jdbc回滚demo的更多相关文章

  1. Spring事务不回滚原因分析

    Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离. 在我完成一个项目的时候,遇到了一个Spring事务不回滚的问题,通过aspectJ和@Transacti ...

  2. Spring事务管理——回滚(rollback-for)控制

    探讨Spring事务控制中,异常触发事务回滚原理.文章进行了6种情况下的Spring事务是否回滚. 以下代码都是基于Spring与Mybatis整合,使用Spring声明式事务配置事务方法. 1.不捕 ...

  3. Spring事务管理回滚问题

    Spring事务管理不能回滚问题 在前段时间学习SpringMVC的练习中,碰到声明式事务管理时,事务不能回滚的情况,通过查看博客和资料,解决了问题. 原因 导致Spring事务管理不能回滚的原因有两 ...

  4. spring 事物不回滚

    使用spring控制事物,为什么有些情况事物,事物不回滚呢?? 默认spring事务只在发生未被捕获的 RuntimeException时才回滚.   spring aop  异常捕获原理: 被拦截的 ...

  5. Spring @Transactional ——事务回滚

    工作原理运行配置@Transactional注解的测试类的时候,具体会发生如下步骤1)事务开始时,通过AOP机制,生成一个代理connection对象,并将其放入DataSource实例的某个与Dat ...

  6. Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...

  7. Spring事务异常回滚,捕获异常不抛出就不会回滚

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...

  8. 【转】Spring事务异常回滚,捕获异常不抛出就不会回滚

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......     为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异 ...

  9. Spring 事务不回滚

    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常时候 日志是打印了,但是加的事务却没有回滚. 例:     类似这样的方法不会回滚 (一个方 ...

随机推荐

  1. IIS通过HTML5实现应用程序缓存的离线浏览

    这里我是使用的IIS7: IIS7发布了网站后要想使用HTML5的应用程序缓存,需要增加一个关于文本/缓存清单( text/cache-manifest)的新的MIME类型,选中网站添加一个MIME类 ...

  2. Valid Sudoku leetcode java

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. [Math]理解卡尔曼滤波器 (Understanding Kalman Filter)

    1. 卡尔曼滤波器介绍 卡尔曼滤波器的介绍, 见 Wiki 这篇文章主要是翻译了 Understanding the Basis of the Kalman Filter Via a Simple a ...

  4. Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测

    Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测 2017年12月13日 17:39:11 机器之心V 阅读数:5931   近日,Artur Suilin 等人发布了 Kaggl ...

  5. wifidog接口文档(转)

    目录(?)[-] 网关心跳协议 请求信息 回复格式 例子 用户状态心跳协议 请求格式 注意 回复格式 状态码 例子 跳转协议 请求格式 例子 注册协议 请求格式 例子 wifidog是搭建无线热点认证 ...

  6. linux kernel内存映射实例分析

    作者:JHJ(jianghuijun211@gmail.com)日期:2012/08/24 欢迎转载,请注明出处 引子 现在android智能手机市场异常火热,硬件升级非常迅猛,arm cortex ...

  7. Cognos11中关于CJAP第三方认证的相关配置

    cognos11同样适用于自定义java程序的第三方认证,而且在测试方面给了直观的测试接口,如下图所示 当用户配置好了自定义java程序的认证之后,程序会提示用户输入我们自己的认证库用户信息例如adm ...

  8. 前端框架 Vue 初探

    一.前言 前几日使用微信网页版时,好奇这个网页用了什么前端框架.用Chrome的开发人员模式一探到底,发现原来用了一个名叫 Angular 的框架.好吧,既然微信用了.那我也最好还是看看.等等,你这篇 ...

  9. java 流媒体服务器Red5 FQA

    原文链接:http://www.cnblogs.com/zhuzhao/archive/2008/08/12/1265661.html red5 FQA   red5 FQA 引自:http://hi ...

  10. SpringBoot报错 : Whitelabel Error Page

    添加了一个Controller类,本来想试下Spring MVC是否可以正常运行,结果报错,Controller类的内容: @RestController public class Test1Cont ...