spring-framework / org.springframework.test.context.transaction / TestContextTransactionUtils / retrieveDataSource

retrieveDataSource

@Nullable open static fun retrieveDataSource(testContext: TestContext, @Nullable name: String): DataSource

Retrieve the DataSource to use for the supplied TestContext.

The following algorithm is used to retrieve the DataSource from the org.springframework.context.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

Exceptions

BeansException - if an error occurs while retrieving an explicitly named DataSource

Return
the DataSource to use, or null if not found