配置测试类
  添加如下内容在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. Tensorflow 模型持久化saver及加载图结构

    主要内容: 1. 直接保存,加载模型; (可以指定加载,保存的var_list) 2. 加载,保存指定变量的模型 3. slim加载模型使用 4. 加载模型图结构和参数等 tensorflow 恢复部 ...

  2. Binary Tree Level Order Traversal II leetcode java

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  3. Spring Boot Maven 打包可执行Jar文件!

    Maven pom.xml 必须包含 <packaging>jar</packaging> <build> <plugins> <plugin&g ...

  4. Gridview 单选效果实现,且用且珍惜

    Gridview 单选效果,在androidapp中实现非常频繁,我这里提供我的一种思路,这是我的思维导图: 怎么样将这样的思维导图翻译成java代码了,请看下面的代码了: // 电子产品的图片列表信 ...

  5. 【大数据】基于Hadoop的HBase的安装(转)

    https://note.youdao.com/share/?id=c27485373a08517f7ad2e7ec901cd8d5&type=note#/ 安装前先确认HBse和Hadoop ...

  6. Jquery 让contains不区分大小写

    默认的Jquery contains方法是区分大小写的,以下方法可以使contains不区分大小写(在匹配时将要匹配的元素内容全部转换成小写再进行匹配) // NEW selector jQuery. ...

  7. 为Subline Text 3 添加支持ini文件语法高亮

    转: http://lancelot.blog.51cto.com/393579/1783653 在Subline text 官网下载了Subline text 3 .不过发现没有对ini格式文件的语 ...

  8. MFC自定义控件如何向父窗口发送自定义消息

    自定义了一个控件 class CHtmlEditCtrlEx : public CHtmlEditCtrl   想在这个控件接收到Ctrl+V键盘消息的时候,向该控件所在的窗口发送一个自定义消息.具体 ...

  9. 红米1线刷救砖教程V5版(移动联通适用,线刷包永久有效)

    红米1线刷救砖教程V5版(移动联通适用,线刷包永久有效) 原文来自:http://www.miui.com/thread-1890972-1-1.html?mobile=2 ,加了些自己的经验. (我 ...

  10. GPUImage API 文档之GPUImagePicture类

    GPUImagePicture类静态图像处理操作,它可以是需要处理的静态图像,也可以是一张作为纹理使用的图片,调用向它发送processImage消息,进行图像滤镜处理. 方法 - (id)initW ...