org.springframework.test
Class AbstractTransactionalSpringContextTests

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended byorg.springframework.test.AbstractSpringContextTests
              extended byorg.springframework.test.AbstractDependencyInjectionSpringContextTests
                  extended byorg.springframework.test.AbstractTransactionalSpringContextTests
All Implemented Interfaces:
Test
Direct Known Subclasses:
AbstractTransactionalDataSourceSpringContextTests

public abstract class AbstractTransactionalSpringContextTests
extends AbstractDependencyInjectionSpringContextTests

Convenient superclass for tests that should occur in a transaction, but normally will roll the transaction back on the completion of each test.

This is useful in a range of circumstances, allowing the following benefits:

This class is typically very fast, compared to traditional setup/teardown scripts.

If data should be left in the database, call the setComplete() method in each test. The defaultRollback() property, which defaults to true, determines whether transactions will complete by default.

Requires a single bean in the context implementing the PlatformTransactionManager interface. This will be set by the superclass's Dependency Injection mechanism. If using the superclass's Field Injection mechanism, the implementation should be named "transactionManager". This mechanism allows the use of this superclass even when there's more than one transaction manager in the context.

Since:
1.1.1
Author:
Rod Johnson

Field Summary
protected  PlatformTransactionManager transactionManager
           
protected  TransactionStatus transactionStatus
          TransactionStatus for this test.
 
Fields inherited from class org.springframework.test.AbstractDependencyInjectionSpringContextTests
applicationContext, managedVariableNames
 
Fields inherited from class org.springframework.test.AbstractSpringContextTests
logger
 
Constructor Summary
AbstractTransactionalSpringContextTests()
           
 
Method Summary
protected  void endTransaction()
          Immediately force a commit or rollback of the transaction, according to the complete flag.
protected  void onSetUp()
          Subclasses can override this method in place of the setUp() method, which is final in this class.
protected  void onSetUpInTransaction()
          Subclasses can override this method to perform any setup operations, such as populating a database table, in the transaction created by this class.
protected  void onTearDown()
          Subclasses can override this to add custom behavior on teardown.
protected  void onTearDownInTransaction()
          Subclasses can override this method to run invariant tests here.
protected  void setComplete()
          Cause the transaction to commit for this test method, even if default is set to rollback.
 void setDefaultRollback(boolean defaultRollback)
          Subclasses can set this value in their constructor to change default, which is always to roll the transaction back
 void setTransactionManager(PlatformTransactionManager ptm)
          Populated by dependency injection by superclass.
 
Methods inherited from class org.springframework.test.AbstractDependencyInjectionSpringContextTests
contextKey, getConfigLocations, getLoadCount, initManagedVariableNames, isPopulateProtectedVariables, loadContextLocations, populateProtectedVariables, setDirty, setPopulateProtectedVariables, setUp, tearDown
 
Methods inherited from class org.springframework.test.AbstractSpringContextTests
contextKeyString, getContext, hasCachedContext, loadContext, setDirty
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

transactionManager

protected PlatformTransactionManager transactionManager

transactionStatus

protected TransactionStatus transactionStatus
TransactionStatus for this test. Typical subclasses won't need to use it.

Constructor Detail

AbstractTransactionalSpringContextTests

public AbstractTransactionalSpringContextTests()
Method Detail

setDefaultRollback

public void setDefaultRollback(boolean defaultRollback)
Subclasses can set this value in their constructor to change default, which is always to roll the transaction back


setTransactionManager

public void setTransactionManager(PlatformTransactionManager ptm)
Populated by dependency injection by superclass.


onSetUp

protected final void onSetUp()
                      throws Exception
Description copied from class: AbstractDependencyInjectionSpringContextTests
Subclasses can override this method in place of the setUp() method, which is final in this class. This implementation does nothing.

Overrides:
onSetUp in class AbstractDependencyInjectionSpringContextTests
Throws:
Exception

onSetUpInTransaction

protected void onSetUpInTransaction()
                             throws Exception
Subclasses can override this method to perform any setup operations, such as populating a database table, in the transaction created by this class.

Throws:
Exception - simply let any exception propagate

onTearDown

protected final void onTearDown()
                         throws Exception
Description copied from class: AbstractDependencyInjectionSpringContextTests
Subclasses can override this to add custom behavior on teardown.

Overrides:
onTearDown in class AbstractDependencyInjectionSpringContextTests
Throws:
Exception

onTearDownInTransaction

protected void onTearDownInTransaction()
Subclasses can override this method to run invariant tests here. The transaction is still open, so any changes made in the transaction will still be visible. There is no need to clean up the database, as rollback will follow automatically.


setComplete

protected void setComplete()
Cause the transaction to commit for this test method, even if default is set to rollback.


endTransaction

protected void endTransaction()
Immediately force a commit or rollback of the transaction, according to the complete flag.

Can be used to explicitly let the transaction end early, for example to check whether lazy associations of persistent objects work outside of a transaction (i.e. have been initialized properly).

See Also:
setComplete()


Copyright (C) 2003-2004 The Spring Framework Project.