类 TransactionalTestExecutionListener
- 所有已实现的接口:
cn.taketoday.core.Ordered,TestExecutionListener
TestExecutionListener that provides support for executing tests
within test-managed transactions by honoring Framework'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
Infra-managed transactions (i.e., those managed directly
by Infra within the ApplicationContext loaded for tests) or
application-managed transactions (i.e., those managed
programmatically within application code that is invoked via tests).
Infra-managed and application-managed transactions will typically
participate in test-managed transactions; however, caution should be
taken if Infra-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
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.
@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 Infra
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(cn.taketoday.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
| Attribute | Supported for test-managed transactions |
|---|---|
value and transactionManager | yes |
propagation |
only NOT_SUPPORTED
and NEVER are supported |
isolation | no |
timeout | no |
readOnly | no |
rollbackFor and rollbackForClassName |
no: use TestTransaction.flagForRollback() instead |
noRollbackFor and noRollbackForClassName |
no: use TestTransaction.flagForCommit() instead |
- 从以下版本开始:
- 4.0
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
-
TransactionManagementConfigurerTransactionalCommitRollbackBeforeTransactionAfterTransactionTestTransaction
-
字段概要
字段修饰符和类型字段说明protected final cn.taketoday.transaction.interceptor.TransactionAttributeSourceprivate static final cn.taketoday.logging.Logger从接口继承的字段 cn.taketoday.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明voidafterTestMethod(TestContext testContext) If a transaction is currently active for the supplied test context, this method will end the transaction and run@AfterTransactionmethods.voidbeforeTestMethod(TestContext testContext) If the test method of the supplied test context is configured to run within a transaction, this method will run@BeforeTransactionmethods and start a new transaction.getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) Get all methods in the suppliedclassand its superclasses which are annotated with the suppliedannotationTypebut which are not shadowed by methods overridden in subclasses.final intgetOrder()Returns4000.protected cn.taketoday.transaction.PlatformTransactionManagergetTransactionManager(TestContext testContext) Get the transaction manager to use for the supplied test context.protected cn.taketoday.transaction.PlatformTransactionManagergetTransactionManager(TestContext testContext, String qualifier) Get the transaction manager to use for the supplied test context andqualifier.protected final booleanisDefaultRollback(TestContext testContext) Determine whether or not to rollback transactions by default for the supplied test context.protected final booleanisRollback(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@Rollbackannotation.protected voidrunAfterTransactionMethods(TestContext testContext) Run all@AfterTransactionmethods for the specified test context.protected voidrunBeforeTransactionMethods(TestContext testContext) Run all@BeforeTransactionmethods for the specified test context.从类继承的方法 cn.taketoday.test.context.support.AbstractTestExecutionListener
afterTestClass, afterTestExecution, beforeTestClass, beforeTestExecution, prepareTestInstance
-
字段详细资料
-
logger
private static final cn.taketoday.logging.Logger logger -
attributeSource
protected final cn.taketoday.transaction.interceptor.TransactionAttributeSource attributeSource
-
-
构造器详细资料
-
TransactionalTestExecutionListener
public TransactionalTestExecutionListener()
-
-
方法详细资料
-
getOrder
public final int getOrder()Returns4000.- 指定者:
getOrder在接口中cn.taketoday.core.Ordered- 覆盖:
getOrder在类中AbstractTestExecutionListener
-
beforeTestMethod
If the test method of the supplied test context is configured to run within a transaction, this method will run@BeforeTransactionmethods and start a new transaction.Note that if a
@BeforeTransactionmethod fails, any remaining@BeforeTransactionmethods will not be invoked, and a transaction will not be started.- 指定者:
beforeTestMethod在接口中TestExecutionListener- 覆盖:
beforeTestMethod在类中AbstractTestExecutionListener- 参数:
testContext- the test context in which the test method will be executed; nevernull- 抛出:
Exception- allows any exception to propagate- 另请参阅:
-
@TransactionalgetTransactionManager(TestContext, String)
-
afterTestMethod
If a transaction is currently active for the supplied test context, this method will end the transaction and run@AfterTransactionmethods.@AfterTransactionmethods are guaranteed to be invoked even if an error occurs while ending the transaction.- 指定者:
afterTestMethod在接口中TestExecutionListener- 覆盖:
afterTestMethod在类中AbstractTestExecutionListener- 参数:
testContext- the test context in which the test method was executed; nevernull- 抛出:
Exception- allows any exception to propagate- 另请参阅:
-
runBeforeTransactionMethods
Run all@BeforeTransactionmethods for the specified test context. If one of the methods fails, however, the caught exception will be rethrown in a wrappedRuntimeException, and the remaining methods will not be given a chance to execute.- 参数:
testContext- the current test context- 抛出:
Exception
-
runAfterTransactionMethods
Run all@AfterTransactionmethods 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 context- 抛出:
Exception
-
getTransactionManager
@Nullable protected cn.taketoday.transaction.PlatformTransactionManager getTransactionManager(TestContext testContext, @Nullable String qualifier) Get the transaction manager to use for the supplied test context andqualifier.Delegates to
getTransactionManager(TestContext)if the suppliedqualifierisnullor empty.- 参数:
testContext- the test context for which the transaction manager should be retrievedqualifier- the qualifier for selecting between multiple bean matches; may benullor empty- 返回:
- the transaction manager to use, or
nullif not found - 抛出:
cn.taketoday.beans.BeansException- if an error occurs while retrieving the transaction manager- 另请参阅:
-
getTransactionManager
@Nullable protected cn.taketoday.transaction.PlatformTransactionManager getTransactionManager(TestContext testContext) Get the transaction manager to use for the supplied test context.The default implementation simply delegates to
TestContextTransactionUtils.retrieveTransactionManager(cn.taketoday.test.context.TestContext, java.lang.String).- 参数:
testContext- the test context for which the transaction manager should be retrieved- 返回:
- the transaction manager to use, or
nullif not found - 抛出:
cn.taketoday.beans.BeansException- if an error occurs while retrieving an explicitly named transaction managerIllegalStateException- if more than one TransactionManagementConfigurer exists in the ApplicationContext- 另请参阅:
-
isDefaultRollback
Determine whether or not to rollback transactions by default for the supplied test context.- 参数:
testContext- the test context for which the default rollback flag should be retrieved- 返回:
- the default rollback flag for the supplied test context
- 抛出:
Exception- if an error occurs while determining the default rollback flag
-
isRollback
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@Rollbackannotation.- 参数:
testContext- the test context for which the rollback flag should be retrieved- 返回:
- the rollback flag for the supplied test context
- 抛出:
Exception- if an error occurs while determining the rollback flag
-
getAnnotatedMethods
private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) Get all methods in the suppliedclassand its superclasses which are annotated with the suppliedannotationTypebut which are not shadowed by methods overridden in subclasses.Default methods on interfaces are also detected.
- 参数:
clazz- the class for which to retrieve the annotated methodsannotationType- the annotation type for which to search- 返回:
- all annotated methods in the supplied class and its superclasses as well as annotated interface default methods
-