类 TransactionalTestExecutionListener
- java.lang.Object
- org.springframework.test.context.support.AbstractTestExecutionListener
- org.springframework.test.context.transaction.TransactionalTestExecutionListener
- 所有已实现的接口:
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 theApplicationContext
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 thanREQUIRED
orSUPPORTS
.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 thepropagation
type set toNOT_SUPPORTED
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 aPlatformTransactionManager
bean to be defined in the SpringApplicationContext
for the test. In case there are multiple instances ofPlatformTransactionManager
within the test'sApplicationContext
, a qualifier may be declared via@Transactional
(e.g.,@Transactional("myTxMgr")
or@Transactional(transactionManger = "myTxMgr")
, orTransactionManagementConfigurer
can be implemented by an@Configuration
class. SeeTestContextTransactionUtils.retrieveTransactionManager(org.springframework.test.context.TestContext, java.lang.String)
for details on the algorithm used to look up a transaction manager in the test'sApplicationContext
.- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
TransactionManagementConfigurer
,Transactional
,Commit
,Rollback
,BeforeTransaction
,AfterTransaction
,TestTransaction
字段概要
字段 修饰符和类型 字段 说明 protected TransactionAttributeSource
attributeSource
从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 TransactionalTestExecutionListener()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterTestMethod(TestContext testContext)
If a transaction is currently active for 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.int
getOrder()
Returns4000
.protected PlatformTransactionManager
getTransactionManager(TestContext testContext)
Get the transaction manager to use for the supplied test context.protected PlatformTransactionManager
getTransactionManager(TestContext testContext, String qualifier)
protected boolean
isDefaultRollback(TestContext testContext)
Determine whether or not to rollback transactions by default for the supplied test context.protected 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.protected void
runAfterTransactionMethods(TestContext testContext)
Run all@AfterTransaction
methods for the specified test context.protected void
runBeforeTransactionMethods(TestContext testContext)
Run all@BeforeTransaction
methods for the specified test context.从类继承的方法 org.springframework.test.context.support.AbstractTestExecutionListener
afterTestClass, beforeTestClass, prepareTestInstance
字段详细资料
attributeSource
protected final TransactionAttributeSource attributeSource
构造器详细资料
TransactionalTestExecutionListener
public TransactionalTestExecutionListener()
方法详细资料
getOrder
public final int getOrder()
Returns4000
.- 指定者:
getOrder
在接口中Ordered
- 覆盖:
getOrder
在类中AbstractTestExecutionListener
- 返回:
- the order value
- 另请参阅:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
beforeTestMethod
public void beforeTestMethod(TestContext testContext) throws Exception
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, any remaining@BeforeTransaction
methods 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- 另请参阅:
Transactional
,getTransactionManager(TestContext, String)
afterTestMethod
public void afterTestMethod(TestContext testContext) throws Exception
If a transaction is currently active for 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.- 指定者:
afterTestMethod
在接口中TestExecutionListener
- 覆盖:
afterTestMethod
在类中AbstractTestExecutionListener
- 参数:
testContext
- the test context in which the test method was executed; nevernull
- 抛出:
Exception
- allows any exception to propagate
runBeforeTransactionMethods
protected void runBeforeTransactionMethods(TestContext testContext) throws Exception
Run all@BeforeTransaction
methods 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
protected void runAfterTransactionMethods(TestContext testContext) throws Exception
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.- 参数:
testContext
- the current test context- 抛出:
Exception
getTransactionManager
protected PlatformTransactionManager getTransactionManager(TestContext testContext, String qualifier)
Get the transaction manager to use for the supplied test context andqualifier
.Delegates to
getTransactionManager(TestContext)
if the suppliedqualifier
isnull
or empty.- 参数:
testContext
- the test context for which the transaction manager should be retrievedqualifier
- the qualifier for selecting between multiple bean matches; may benull
or empty- 返回:
- the transaction manager to use, or
null
if not found - 抛出:
BeansException
- if an error occurs while retrieving the transaction manager- 另请参阅:
getTransactionManager(TestContext)
getTransactionManager
protected PlatformTransactionManager getTransactionManager(TestContext testContext)
Get the transaction manager to use for the supplied test context.The default implementation simply delegates to
TestContextTransactionUtils.retrieveTransactionManager(org.springframework.test.context.TestContext, java.lang.String)
.- 参数:
testContext
- the test context for which the transaction manager should be retrieved- 返回:
- the transaction manager to use, or
null
if not found - 抛出:
BeansException
- if an error occurs while retrieving an explicitly named transaction managerIllegalStateException
- if more than one TransactionManagementConfigurer exists in the ApplicationContext- 另请参阅:
getTransactionManager(TestContext, String)
isDefaultRollback
protected final boolean isDefaultRollback(TestContext testContext) throws Exception
Determine whether or not to rollback transactions by default for the supplied test context.Supports
@Rollback
,@Commit
, or@TransactionConfiguration
at the class-level.- 参数:
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
protected final boolean isRollback(TestContext testContext) throws Exception
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.- 参数:
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