public class

TransactionalTestExecutionListener

extends AbstractTestExecutionListener
java.lang.Object
   ↳ org.springframework.test.context.support.AbstractTestExecutionListener
     ↳ org.springframework.test.context.transaction.TransactionalTestExecutionListener

Class Overview

TestExecutionListener which provides support for executing tests within transactions by using @Transactional and @NotTransactional annotations.

Changes to the database during a test run with @Transactional will be run within a transaction that will, by default, be automatically rolled back after completion of the test; whereas, changes to the database during a test run with @NotTransactional will not be run within a transaction. Similarly, test methods that are not annotated with either @Transactional (at the class or method level) or @NotTransactional will not be run within a transaction.

Transactional commit and rollback behavior can be configured via the class-level @TransactionConfiguration and method-level @Rollback annotations. @TransactionConfiguration also provides configuration of the bean name of the PlatformTransactionManager that is to be used to drive transactions.

When executing transactional tests, it is sometimes useful to be able execute certain set up or tear down code outside of a transaction. TransactionalTestExecutionListener provides such support for methods annotated with @BeforeTransaction and @AfterTransaction.

Summary

Fields
protected final TransactionAttributeSource attributeSource
Public Constructors
TransactionalTestExecutionListener()
Public Methods
void afterTestMethod(TestContext testContext)
If a transaction is currently active for the test method of the supplied test context, this method will end the transaction and run @AfterTransaction methods.
void beforeTestMethod(TestContext testContext)
If the test method of the supplied test context is configured to run within a transaction, this method will run @BeforeTransaction methods and start a new transaction.
Protected Methods
final PlatformTransactionManager getTransactionManager(TestContext testContext)
Get the transaction manager to use for the supplied test context.
final boolean isDefaultRollback(TestContext testContext)
Determine whether or not to rollback transactions by default for the supplied test context.
final boolean isRollback(TestContext testContext)
Determine whether or not to rollback transactions for the supplied test context by taking into consideration the default rollback flag and a possible method-level override via the Rollback annotation.
void runAfterTransactionMethods(TestContext testContext)
Run all @AfterTransaction methods for the specified test context.
void runBeforeTransactionMethods(TestContext testContext)
Run all @BeforeTransaction methods for the specified test context.
[Expand]
Inherited Methods
From class org.springframework.test.context.support.AbstractTestExecutionListener
From class java.lang.Object
From interface org.springframework.test.context.TestExecutionListener

Fields

protected final TransactionAttributeSource attributeSource

Public Constructors

public TransactionalTestExecutionListener ()

Public Methods

public void afterTestMethod (TestContext testContext)

If a transaction is currently active for the test method of the supplied test context, this method will end the transaction and run @AfterTransaction methods.

@AfterTransaction methods are guaranteed to be invoked even if an error occurs while ending the transaction.

Parameters
testContext the test context in which the test method was executed; never null
Throws
Exception

public void beforeTestMethod (TestContext testContext)

If the test method of the supplied test context is configured to run within a transaction, this method will run @BeforeTransaction methods and start a new transaction.

Note that if a @BeforeTransaction method fails, remaining @BeforeTransaction methods will not be invoked, and a transaction will not be started.

Parameters
testContext the test context in which the test method will be executed; never null
Throws
Exception

Protected Methods

protected final PlatformTransactionManager getTransactionManager (TestContext testContext)

Get the transaction manager to use for the supplied test context.

Parameters
testContext the test context for which the transaction manager should be retrieved
Returns
  • the transaction manager to use, or null if not found
Throws
BeansException if an error occurs while retrieving the transaction manager

protected final boolean isDefaultRollback (TestContext testContext)

Determine whether or not to rollback transactions by default for the supplied test context.

Parameters
testContext the test context for which the default rollback flag should be retrieved
Returns
  • the default rollback flag for the supplied test context
Throws
Exception if an error occurs while determining the default rollback flag

protected final boolean isRollback (TestContext testContext)

Determine whether or not to rollback transactions for the supplied test context by taking into consideration the default rollback flag and a possible method-level override via the Rollback annotation.

Parameters
testContext the test context for which the rollback flag should be retrieved
Returns
  • the rollback flag for the supplied test context
Throws
Exception if an error occurs while determining the rollback flag

protected void runAfterTransactionMethods (TestContext testContext)

Run all @AfterTransaction methods for the specified test context. If one of the methods fails, the caught exception will be logged as an error, and the remaining methods will be given a chance to execute. After all methods have executed, the first caught exception, if any, will be rethrown.

Parameters
testContext the current test context
Throws
Exception

protected void runBeforeTransactionMethods (TestContext testContext)

Run all @BeforeTransaction methods for the specified test context. If one of the methods fails, however, the caught exception will be rethrown in a wrapped RuntimeException, and the remaining methods will not be given a chance to execute.

Parameters
testContext the current test context
Throws
Exception