配置测试类
  添加如下内容在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. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  2. 安装Lync 2013过程中遇到的第一个报错

    安装Lync 2013, 首先要去做的就是prepare AD Forest. 在使用向导的时候会遇到报错如下: Prepare Forest Active Directory setting exe ...

  3. mysql分布式数据库中间件对比

    目前数据库中间件有很多,基本这些中间件在下都有了解和使用,各种中间件优缺点及使用场景也都有些心的.所以总结一个关于中间件比较的系列,希望可以对大家有帮助. 1. 什么是中间件 传统的架构模式就是 应用 ...

  4. Python3爬虫:利用Fidder抓取手机APP的数据

    1.什么是Fiddler? Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,ht ...

  5. FastText 文本分类使用心得

    http://blog.csdn.net/thriving_fcl/article/details/53239856 最近在一个项目里使用了fasttext[1], 这是facebook今年开源的一个 ...

  6. 一文学会用 Tensorflow 搭建神经网络

    http://www.jianshu.com/p/e112012a4b2d 本文是学习这个视频课程系列的笔记,课程链接是 youtube 上的,讲的很好,浅显易懂,入门首选, 而且在github有代码 ...

  7. echarts文档对照

    echarts的各种配置项可以对照这个文档: https://echarts.baidu.com/echarts2/doc/option.html#title~tooltip.axisPointer. ...

  8. OpenGL book list

      From: https://www.codeproject.com/Articles/771225/Learning-Modern-OpenGL   A little guide about mo ...

  9. HTTPS证书撤销

    如果浏览器信息被拦截,可以选择清洗掉之前的证书 关闭浏览器,在CMD中输入命令 certutil -urlcache * certutil -urlcache * delete certutil -u ...

  10. mysql设置允许外网访问

    1.设置mysql服务允许外网访问 修改mysql的配置文件,有的是my.ini,有的是my.cnf[linux],找到bind-address变量,这个值默认是127.0.0.1,设置为0.0.0. ...