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 Summary
Modifier and TypeFieldDescriptionstatic final String
Default bean name for aDataSource
:"dataSource"
.static final String
Default bean name for aPlatformTransactionManager
:"transactionManager"
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic TransactionAttribute
createDelegatingTransactionAttribute
(TestContext testContext, TransactionAttribute targetAttribute) Create a delegatingTransactionAttribute
for the supplied targetTransactionAttribute
andTestContext
, using the names of the test class and test method to build the name of the transaction.static TransactionAttribute
createDelegatingTransactionAttribute
(TestContext testContext, TransactionAttribute targetAttribute, boolean includeMethodName) Create a delegatingTransactionAttribute
for the supplied targetTransactionAttribute
andTestContext
, using the names of the test class and test method (if requested) to build the name of the transaction.static DataSource
retrieveDataSource
(TestContext testContext, String name) Retrieve theDataSource
to use for the supplied test context.static PlatformTransactionManager
retrieveTransactionManager
(TestContext testContext, String name) Retrieve the transaction manager to use for the supplied test context.
-
Field Details
-
DEFAULT_DATA_SOURCE_NAME
Default bean name for aDataSource
:"dataSource"
.- See Also:
-
DEFAULT_TRANSACTION_MANAGER_NAME
Default bean name for aPlatformTransactionManager
:"transactionManager"
.- See Also:
-
-
Constructor Details
-
TestContextTransactionUtils
public TestContextTransactionUtils()
-
-
Method Details
-
retrieveDataSource
@Nullable public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) Retrieve theDataSource
to use for the supplied test context.The following algorithm is used to retrieve the
DataSource
from theApplicationContext
of the supplied test context:- Look up the
DataSource
by type and name, if the suppliedname
is non-empty, throwing aBeansException
if the namedDataSource
does not exist. - Attempt to look up the single
DataSource
by type. - Attempt to look up the primary
DataSource
by type. - Attempt to look up the
DataSource
by type and the default data source name.
- Parameters:
testContext
- the test context for which theDataSource
should be retrieved; nevernull
name
- the name of theDataSource
to retrieve (may benull
or empty)- Returns:
- the
DataSource
to use, ornull
if not found - Throws:
BeansException
- if an error occurs while retrieving an explicitly namedDataSource
- Look up the
-
retrieveTransactionManager
@Nullable public static 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
ApplicationContext
of the supplied test context:- Look up the transaction manager by type and explicit name, if the supplied
name
is non-empty, throwing aBeansException
if 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; nevernull
name
- the name of the transaction manager to retrieve (may benull
or empty)- Returns:
- the transaction manager to use, or
null
if not found - Throws:
BeansException
- if an error occurs while retrieving an explicitly named transaction managerIllegalStateException
- if more than one TransactionManagementConfigurer exists in the ApplicationContext
- Look up the transaction manager by type and explicit name, if the supplied
-
createDelegatingTransactionAttribute
public static TransactionAttribute createDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute) Create a delegatingTransactionAttribute
for the supplied targetTransactionAttribute
andTestContext
, using the names of the test class and test method to build the name of the transaction.- Parameters:
testContext
- theTestContext
upon which to base the nametargetAttribute
- theTransactionAttribute
to delegate to- Returns:
- the delegating
TransactionAttribute
-
createDelegatingTransactionAttribute
public static TransactionAttribute createDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute, boolean includeMethodName) Create a delegatingTransactionAttribute
for the supplied targetTransactionAttribute
andTestContext
, using the names of the test class and test method (if requested) to build the name of the transaction.- Parameters:
testContext
- theTestContext
upon which to base the nametargetAttribute
- theTransactionAttribute
to delegate toincludeMethodName
-true
if the test method's name should be included in the name of the transaction- Returns:
- the delegating
TransactionAttribute
- Since:
- 6.1
-