Achieve Automatic Transaction Rollback with MockStrutsTestCase and Spring

Spring provides a convenience base class (AbstractTransactionalDataSourceSpringContextTests which extends the TestCase class from Junit) to automatically rollback any updates made to the database at the end of a test method. This works great when integration testing Service and DAO beans but it wont help when integration testing the Struts layer with MockStrutsTestCase.

Spring does not provide a class such as AbstractTransactionalDataSourceSpringContextTests that extends from the MockStrutsTestCase. This might be due to practical issues with the way Spring context initialization works when using MockStrutsTestCase. I tried achieving the transaction rollback in the same way as was done in the AbstractTransactionalDataSourceSpringContextTests class but it didn’t work out as the application code started a new transaction and commited as usual without using the transaction I started in my own subclass of MockStrutsTestCase.

Another option left for me was to go down to the Transaction Manager level and achieve the rollbacks there. The application uses Hibernate at the DAO level and it was using HibernateTransactionManager as the transaction manager implementation when running test cases . Therefore I had to write a new class extending from HibernateTransactionManager which overrides the doCommit() method. The overridden method will call doRollback() method in the super class. This would rollback the current running transaction even if the declarative transaction handling code in Spring calls doCommit on the transaction manager.

package com.xyz.txn;import org.springframework.orm.hibernate3.HibernateTransactionManager;

public class HibernateRollbackTxnManager extends HibernateTransactionManager {

/*

  * doCommit method that calls the doRollback method of the super class.

  *

  */

 protected void doCommit(DefaultTransactionStatus txnStatus) {

     super.doRollback(txnStatus);

 }

}

Finally override the default transaction manager in your test spring bean configuration with the rollback only implementation.

  <bean id="txnManager" class="com.com.xyz.txn.HibernateRollbackTxnManager">

      <property name="sessionFactory"><ref local="sessionFactory"></property>

  </bean>

The same strategy would work with other transaction managers such as DataSourceTransactionManager too.

One Reply to “Achieve Automatic Transaction Rollback with MockStrutsTestCase and Spring”

  1. Hi RAJESHKUMAR,I’m Shyju working in an IT firm in Dubai. I have find your blog which is very ifonrmative and I’m sure you have good knowledge in Oracle. I’m very new to Oracle and E-Business Suite. Here we have installed e-business suite 12 and Vision Database in a Windows 2003 machine. Now I want to access the Vision database. I can login to e-business suite with user which is there in Vision database(ie. username: cbrown and password:welcome).,and I have got sysadmin access also, but I could not find the table where the user cbrown resides. Can you give me some suggestions to find that table (using SQL+ or any other oracle client).

Leave a Reply

Your email address will not be published. Required fields are marked *