| org.springframework.test.ConditionalTestCase org.springframework.test.AbstractSpringContextTests org.springframework.test.AbstractSingleSpringContextTests org.springframework.test.AbstractDependencyInjectionSpringContextTests org.springframework.test.AbstractTransactionalSpringContextTests
All known Subclasses: org.springframework.test.AbstractTransactionalDataSourceSpringContextTests,
AbstractTransactionalSpringContextTests | abstract public class AbstractTransactionalSpringContextTests extends AbstractDependencyInjectionSpringContextTests (Code) | | Convenient base class 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:
- Ability to delete or insert any data in the database, without affecting other tests
- Providing a transactional context for any code requiring a transaction
- Ability to write anything to the database without any need to clean up.
This class is typically very fast, compared to traditional setup/teardown scripts.
If data should be left in the database, call the
AbstractTransactionalSpringContextTests.setComplete() method in each test. The
AbstractTransactionalSpringContextTests.setDefaultRollback "defaultRollback" property,
which defaults to "true", determines whether transactions will complete by default.
It is even possible to end the transaction early; for example, to verify lazy
loading behavior of an O/R mapping tool. (This is a valuable away to avoid
unexpected errors when testing a web UI, for example.) Simply call the
AbstractTransactionalSpringContextTests.endTransaction() method. Execution will then occur without a
transactional context.
The
AbstractTransactionalSpringContextTests.startNewTransaction() method may be called after a call to
AbstractTransactionalSpringContextTests.endTransaction() if you wish to create a new transaction, quite
independent of the old transaction. The new transaction's default fate will be to
roll back, unless
AbstractTransactionalSpringContextTests.setComplete() is called again during the scope of the
new transaction. Any number of transactions may be created and ended in this way.
The final transaction will automatically be rolled back when the test case is
torn down.
Transactional behavior requires a single bean in the context implementing the
org.springframework.transaction.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 the
AbstractDependencyInjectionSpringContextTests superclass even
when there is more than one transaction manager in the context.
This base class can also be used without transaction management, if no
PlatformTransactionManager bean is found in the context provided.
Be careful about using this mode, as it allows the potential to permanently modify
data. This mode is available only if dependency checking is turned off in the
AbstractDependencyInjectionSpringContextTests superclass. The non-transactional
capability is provided to enable use of the same subclass in different environments.
author: Rod Johnson author: Juergen Hoeller since: 1.1.1 |
transactionDefinition | protected TransactionDefinition transactionDefinition(Code) | | Transaction definition used by this test class: by default, a plain
DefaultTransactionDefinition. Subclasses can change this to cause different behavior.
|
transactionStatus | protected TransactionStatus transactionStatus(Code) | | TransactionStatus for this test. Typical subclasses won't need to use it.
|
AbstractTransactionalSpringContextTests | public AbstractTransactionalSpringContextTests()(Code) | | Default constructor for AbstractTransactionalSpringContextTests.
|
AbstractTransactionalSpringContextTests | public AbstractTransactionalSpringContextTests(String name)(Code) | | Constructor for AbstractTransactionalSpringContextTests with a JUnit name.
|
endTransaction | protected void endTransaction()(Code) | | 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 (that is, have been initialized properly).
See Also: AbstractTransactionalSpringContextTests.setComplete() |
onTearDownAfterTransaction | protected void onTearDownAfterTransaction() throws Exception(Code) | | Subclasses can override this method to perform cleanup after a transaction
here. At this point, the transaction is not active anymore.
throws: Exception - simply let any exception propagate |
onTearDownInTransaction | protected void onTearDownInTransaction() throws Exception(Code) | | Subclasses can override this method to run invariant tests here.
The transaction is still active at this point, so any changes
made in the transaction will still be visible. However, there is no need
to clean up the database, as a rollback will follow automatically.
NB: Not called if there is no actual transaction, for example
due to no transaction manager being provided in the application context.
throws: Exception - simply let any exception propagate |
setComplete | protected void setComplete()(Code) | | Cause the transaction to commit for this test method,
even if default is set to rollback.
throws: IllegalStateException - if the operation cannot be set tocomplete as no transaction manager was provided |
setDefaultRollback | public void setDefaultRollback(boolean defaultRollback)(Code) | | Subclasses can set this value in their constructor to change
default, which is always to roll the transaction back.
|
setTransactionManager | public void setTransactionManager(PlatformTransactionManager transactionManager)(Code) | | Specify the transaction manager to use. No transaction management will be available
if this is not set. Populated through dependency injection by the superclass.
This mode works only if dependency checking is turned off in the
AbstractDependencyInjectionSpringContextTests superclass.
|
Fields inherited from org.springframework.test.AbstractDependencyInjectionSpringContextTests | final public static int AUTOWIRE_BY_NAME(Code)(Java Doc) final public static int AUTOWIRE_BY_TYPE(Code)(Java Doc) final public static int AUTOWIRE_NO(Code)(Java Doc)
|
Fields inherited from org.springframework.test.ConditionalTestCase | final protected Log logger(Code)(Java Doc)
|
|
|