public class MergeTransactionalTestExecutionListener
extends org.springframework.test.context.support.AbstractTestExecutionListener
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
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 TransactionalTestExecutionListener provides such
support for methods annotated with
@BeforeTransaction and
@AfterTransaction.
This implementation will only wrap those test methods that are explicitly annotated with the Transactional annotation.
TransactionConfiguration,
Transactional,
org.springframework.test.annotation.NotTransactional,
Rollback,
BeforeTransaction,
AfterTransaction| Modifier and Type | Field and Description |
|---|---|
protected org.springframework.transaction.interceptor.TransactionAttributeSource |
attributeSource |
| Constructor and Description |
|---|
MergeTransactionalTestExecutionListener() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterTestMethod(org.springframework.test.context.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(org.springframework.test.context.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 org.springframework.transaction.PlatformTransactionManager |
getTransactionManager(org.springframework.test.context.TestContext testContext)
Get the
transaction manager to use
for the supplied test context. |
protected boolean |
isDefaultRollback(org.springframework.test.context.TestContext testContext)
Determine whether or not to rollback transactions by default for the
supplied
test context. |
protected boolean |
isRollback(org.springframework.test.context.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. |
protected void |
runAfterTransactionMethods(org.springframework.test.context.TestContext testContext)
Run all
@AfterTransaction methods for the
specified test context. |
protected void |
runBeforeTransactionMethods(org.springframework.test.context.TestContext testContext)
Run all
@BeforeTransaction methods for the
specified test context. |
protected final org.springframework.transaction.interceptor.TransactionAttributeSource attributeSource
public MergeTransactionalTestExecutionListener()
public void beforeTestMethod(org.springframework.test.context.TestContext testContext)
throws Exception
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.
beforeTestMethod in interface org.springframework.test.context.TestExecutionListenerbeforeTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListenerExceptionTransactional,
org.springframework.test.annotation.NotTransactionalpublic void afterTestMethod(org.springframework.test.context.TestContext testContext)
throws Exception
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.
afterTestMethod in interface org.springframework.test.context.TestExecutionListenerafterTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListenerExceptionprotected void runBeforeTransactionMethods(org.springframework.test.context.TestContext testContext)
throws Exception
@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.testContext - the current test contextExceptionprotected void runAfterTransactionMethods(org.springframework.test.context.TestContext testContext)
throws Exception
@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.testContext - the current test contextExceptionprotected final org.springframework.transaction.PlatformTransactionManager getTransactionManager(org.springframework.test.context.TestContext testContext)
transaction manager to use
for the supplied test context.testContext - the test context for which the transaction manager
should be retrievednull if not foundorg.springframework.beans.BeansException - if an error occurs while retrieving the transaction managerprotected final boolean isDefaultRollback(org.springframework.test.context.TestContext testContext)
throws Exception
test context.testContext - the test context for which the default rollback flag
should be retrievedException - if an error occurs while determining the default rollback flagprotected final boolean isRollback(org.springframework.test.context.TestContext testContext)
throws Exception
test context by taking into consideration the
default rollback flag and a
possible method-level override via the Rollback annotation.testContext - the test context for which the rollback flag
should be retrievedException - if an error occurs while determining the rollback flagCopyright © 2020. All rights reserved.