Class TestContextTransactionUtils
java.lang.Object
org.springframework.test.context.transaction.TestContextTransactionUtils
Utility methods for working with transactions and data access related beans
 within the Spring TestContext Framework.
 
Mainly for internal use within the framework.
- Since:
- 4.1
- Author:
- Sam Brannen, Juergen Hoeller, Andreas Ahlenstorf
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringDefault bean name for aDataSource:"dataSource".static final StringDefault bean name for aPlatformTransactionManager:"transactionManager".
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic TransactionAttributecreateDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute) Create a delegatingTransactionAttributefor the supplied targetTransactionAttributeandTestContext, using the names of the test class and test method to build the name of the transaction.static TransactionAttributecreateDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute, boolean includeMethodName) Create a delegatingTransactionAttributefor the supplied targetTransactionAttributeandTestContext, using the names of the test class and test method (if requested) to build the name of the transaction.static @Nullable DataSourceretrieveDataSource(TestContext testContext, @Nullable String name) Retrieve theDataSourceto use for the supplied test context.retrieveTransactionManager(TestContext testContext, @Nullable String name) Retrieve the transaction manager to use for the supplied test context.
- 
Field Details- 
DEFAULT_DATA_SOURCE_NAMEDefault bean name for aDataSource:"dataSource".- See Also:
 
- 
DEFAULT_TRANSACTION_MANAGER_NAMEDefault bean name for aPlatformTransactionManager:"transactionManager".- See Also:
 
 
- 
- 
Constructor Details- 
TestContextTransactionUtilspublic TestContextTransactionUtils()
 
- 
- 
Method Details- 
retrieveDataSourcepublic static @Nullable DataSource retrieveDataSource(TestContext testContext, @Nullable String name) Retrieve theDataSourceto use for the supplied test context.The following algorithm is used to retrieve the DataSourcefrom theApplicationContextof the supplied test context:- Look up the DataSourceby type and name, if the suppliednameis non-empty, throwing aBeansExceptionif the namedDataSourcedoes not exist.
- Attempt to look up the single DataSourceby type.
- Attempt to look up the primary DataSourceby type.
- Attempt to look up the DataSourceby type and the default data source name.
 - Parameters:
- testContext- the test context for which the- DataSourceshould be retrieved; never- null
- name- the name of the- DataSourceto retrieve (may be- nullor empty)
- Returns:
- the DataSourceto use, ornullif not found
- Throws:
- BeansException- if an error occurs while retrieving an explicitly named- DataSource
 
- Look up the 
- 
retrieveTransactionManagerpublic static @Nullable PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) Retrieve the transaction manager to use for the supplied test context.The following algorithm is used to retrieve the transaction manager from the ApplicationContextof the supplied test context:- Look up the transaction manager by type and explicit name, if the supplied
 nameis non-empty, throwing aBeansExceptionif the named transaction manager does not exist.
- Attempt to look up the transaction manager via a
 TransactionManagementConfigurer, if present.
- Attempt to look up the single transaction manager by type.
- Attempt to look up the primary transaction manager by type.
- Attempt to look up the transaction manager by type and the default transaction manager name.
 - Parameters:
- testContext- the test context for which the transaction manager should be retrieved; never- null
- name- the name of the transaction manager to retrieve (may be- nullor empty)
- Returns:
- the transaction manager to use, or nullif not found
- Throws:
- BeansException- if an error occurs while retrieving an explicitly named transaction manager
- IllegalStateException- if more than one TransactionManagementConfigurer exists in the ApplicationContext
 
- Look up the transaction manager by type and explicit name, if the supplied
 
- 
createDelegatingTransactionAttributepublic static TransactionAttribute createDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute) Create a delegatingTransactionAttributefor the supplied targetTransactionAttributeandTestContext, using the names of the test class and test method to build the name of the transaction.- Parameters:
- testContext- the- TestContextupon which to base the name
- targetAttribute- the- TransactionAttributeto delegate to
- Returns:
- the delegating TransactionAttribute
 
- 
createDelegatingTransactionAttributepublic static TransactionAttribute createDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute, boolean includeMethodName) Create a delegatingTransactionAttributefor the supplied targetTransactionAttributeandTestContext, using the names of the test class and test method (if requested) to build the name of the transaction.- Parameters:
- testContext- the- TestContextupon which to base the name
- targetAttribute- the- TransactionAttributeto delegate to
- includeMethodName-- trueif the test method's name should be included in the name of the transaction
- Returns:
- the delegating TransactionAttribute
- Since:
- 6.1
 
 
-