Class TransactionalTestExecutionListener

All Implemented Interfaces:
Ordered, TestExecutionListener

public class TransactionalTestExecutionListener extends AbstractTestExecutionListener
TestExecutionListener that provides support for executing tests within test-managed transactions by honoring Spring's @Transactional annotation.

Test-managed Transactions

Test-managed transactions are transactions that are managed declaratively via this listener or programmatically via TestTransaction. Such transactions should not be confused with Spring-managed transactions (i.e., those managed directly by Spring within the ApplicationContext loaded for tests) or application-managed transactions (i.e., those managed programmatically within application code that is invoked via tests). Spring-managed and application-managed transactions will typically participate in test-managed transactions; however, caution should be taken if Spring-managed or application-managed transactions are configured with any propagation type other than REQUIRED or SUPPORTS.

Enabling and Disabling Transactions

Annotating a test method with @Transactional causes the test to be run within a transaction that will, by default, be automatically rolled back after completion of the test. If a test class is annotated with @Transactional, each test method within that class hierarchy will be run within a transaction. Test methods that are not annotated with @Transactional (at the class or method level) will not be run within a transaction. Furthermore, tests that are annotated with @Transactional but have the propagation type set to NOT_SUPPORTED or NEVER will not be run within a transaction.

Declarative Rollback and Commit Behavior

By default, test transactions will be automatically rolled back after completion of the test; however, transactional commit and rollback behavior can be configured declaratively via the @Commit and @Rollback annotations at the class level and at the method level.

Programmatic Transaction Management

As of Spring Framework 4.1, it is possible to interact with test-managed transactions programmatically via the static methods in TestTransaction. TestTransaction may be used within test methods, before methods, and after methods.

Executing Code outside of a Transaction

When executing transactional tests, it is sometimes useful to be able to execute certain set up or tear down code outside of a transaction. TransactionalTestExecutionListener provides such support for methods annotated with @BeforeTransaction or @AfterTransaction. As of Spring Framework 4.3, @BeforeTransaction and @AfterTransaction may also be declared on Java 8 based interface default methods.

Configuring a Transaction Manager

TransactionalTestExecutionListener expects a PlatformTransactionManager bean to be defined in the Spring ApplicationContext for the test. In case there are multiple instances of PlatformTransactionManager within the test's ApplicationContext, a qualifier may be declared via @Transactional (e.g., @Transactional("myTxMgr") or @Transactional(transactionManager = "myTxMgr"), or TransactionManagementConfigurer can be implemented by an @Configuration class. See TestContextTransactionUtils.retrieveTransactionManager(org.springframework.test.context.TestContext, java.lang.String) for details on the algorithm used to look up a transaction manager in the test's ApplicationContext.

@Transactional Attribute Support

AttributeSupported for test-managed transactions
value and transactionManageryes
propagation only NOT_SUPPORTED and NEVER are supported
rollbackFor and rollbackForClassName no: use TestTransaction.flagForRollback() instead
noRollbackFor and noRollbackForClassName no: use TestTransaction.flagForCommit() instead
Sam Brannen, Juergen Hoeller
See Also: