配置测试类
  添加如下内容在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. Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)

    Currently, I was trying to write an ASP.Net application that involved a user clicking a ASP.Net butt ...

  2. Android开发者选项 介绍

    15个必知的Android开发者选项 https://www.jianshu.com/p/07b551ee260b  1.Stay awake 充电时保持屏幕唤醒,开发的时候,时不时的锁屏真是够了,开 ...

  3. Jquery怎么获取select选中项 自定义属性的值

    Jquery如何获取select选中项 自定义属性的值?HTML code <select id="ddl" onchange="ddl_change(this)& ...

  4. JAVA动态编译(JavaCompiler)

    一.简介 在java中javax报下提供了JavaCompiler类,此类可以允许开发人员编译java文件为class文件. 下面示例中是利用JavaCompiler编译文件,并利用URLClassL ...

  5. 基于Deep Learning的中文分词尝试

    http://h2ex.com/1282 现有分词介绍 自然语言处理(NLP,Natural Language Processing)是一个信息时代最重要的技术之一,简单来讲,就是让计算机能够理解人类 ...

  6. 【图片识别】java 图片文字识别 ocr (转)

    http://www.cnblogs.com/inkflower/p/6642264.html 最近在开发的时候需要识别图片中的一些文字,网上找了相关资料之后,发现google有一个离线的工具,以下为 ...

  7. ElasticSearch 5.X 搜索并用高亮显示

    public List<WOSearchModel> searchOrder(OrderSearchReqVO request) throws Exception{List<WOSe ...

  8. DOM元素尺寸offsetWidth,scrollWidth,clientWidth等具体解释

    样例: <div id="div" style="height: 200px;width: 200px;border:solid 50px red;overflow ...

  9. 使maven2在下载依赖包的同时下载其源代码包。

    使maven2在下载依赖包的同时下载其源代码包的方法: 1. 使用maven命令:mvn dependency:sources 下载依赖包的源代码. 2. 使用参数: -DdownloadSource ...

  10. oracle expdp/impdp 用法详解

    http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导 ...