Class TestContextTransactionUtils

java.lang.Object
org.springframework.test.context.transaction.TestContextTransactionUtils

public abstract class TestContextTransactionUtils extends Object
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
  • Field Details

  • Constructor Details

    • TestContextTransactionUtils

      public TestContextTransactionUtils()
  • Method Details

    • retrieveDataSource

      @Nullable public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name)
      Retrieve the DataSource to use for the supplied test context.

      The following algorithm is used to retrieve the DataSource from the ApplicationContext of the supplied test context:

      1. Look up the DataSource by type and name, if the supplied name is non-empty, throwing a BeansException if the named DataSource does not exist.
      2. Attempt to look up the single DataSource by type.
      3. Attempt to look up the primary DataSource by type.
      4. Attempt to look up the DataSource by type and the default data source name.
      Parameters:
      testContext - the test context for which the DataSource should be retrieved; never null
      name - the name of the DataSource to retrieve (may be null or empty)
      Returns:
      the DataSource to use, or null if not found
      Throws:
      BeansException - if an error occurs while retrieving an explicitly named DataSource
    • 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:

      1. Look up the transaction manager by type and explicit name, if the supplied name is non-empty, throwing a BeansException if the named transaction manager does not exist.
      2. Attempt to look up the transaction manager via a TransactionManagementConfigurer, if present.
      3. Attempt to look up the single transaction manager by type.
      4. Attempt to look up the primary transaction manager by type.
      5. 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 null 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 manager
      IllegalStateException - if more than one TransactionManagementConfigurer exists in the ApplicationContext
    • createDelegatingTransactionAttribute

      public static TransactionAttribute createDelegatingTransactionAttribute(TestContext testContext, TransactionAttribute targetAttribute)
      Create a delegating TransactionAttribute for the supplied target TransactionAttribute and TestContext, using the names of the test class and test method to build the name of the transaction.
      Parameters:
      testContext - the TestContext upon which to base the name
      targetAttribute - the TransactionAttribute to delegate to
      Returns:
      the delegating TransactionAttribute