Package org.springframework.test.context.support

# Class AbstractTestContextBootstrapper

java.lang.Object
org.springframework.test.context.support.AbstractTestContextBootstrapper

All Implemented Interfaces:
  `TestContextBootstrapper`

Direct Known Subclasses:
  `DefaultTestContextBootstrapper`

public abstract class AbstractTestContextBootstrapper extends Object implements TestContextBootstrapper
Abstract implementation of the `TestContextBootstrapper` interface which provides most of the behavior required by a bootstrapper.

Concrete subclasses typically will only need to provide implementations for the following methods:

- `getDefaultContextLoaderClass(java.lang.Class<?>)`
- `processMergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration)`

To plug in custom `ContextCache` support, override `getCacheAwareContextLoaderDelegate()`.

Since:
  4.1
Author:
  Sam Brannen, Juergen Hoeller, Phillip Webb

## Constructor Summary

Constructors
Constructor
Description
`AbstractTestContextBootstrapper()`

## Method Summary

Modifier and Type
Method
Description
`final MergedContextConfiguration`
`buildMergedContextConfiguration()`
Build the merged context configuration for the test class in the `BootstrapContext` associated with this bootstrapper.
`TestContext`
`buildTestContext()`
Build a new `DefaultTestContext` using the test class in the `BootstrapContext` associated with this bootstrapper and by delegating to `buildMergedContextConfiguration()` and `getCacheAwareContextLoaderDelegate()`.
`BootstrapContext`
`getBootstrapContext()`
Get the `BootstrapContext` associated with this bootstrapper.
`protected CacheAwareContextLoaderDelegate`
`getCacheAwareContextLoaderDelegate()`
Get the `CacheAwareContextLoaderDelegate` to use for transparent interaction with the `ContextCache`.
`protected List<ContextCustomizerFactory>`
`getContextCustomizerFactories()`
Get the `ContextCustomizerFactory` instances for this bootstrapper.
`protected abstract Class<? extends ContextLoader>`
`getDefaultContextLoaderClass(Class<?> testClass)`
Determine the default `ContextLoader` class to use for the supplied test class.
`protected List<TestExecutionListener>`
`getDefaultTestExecutionListeners()`
Get the default `TestExecutionListeners` for this bootstrapper.
`final List<TestExecutionListener>`
`getTestExecutionListeners()`
Get a list of newly instantiated `TestExecutionListeners` for the test class in the `BootstrapContext` associated with this bootstrapper.
`protected MergedContextConfiguration`
`processMergedContextConfiguration(MergedContextConfiguration mergedConfig)`
Process the supplied, newly instantiated `MergedContextConfiguration` instance.
`protected ContextLoader`
`resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList)`
Resolve the `ContextLoader` class to use for the supplied list of `ContextConfigurationAttributes` and then instantiate and return that `ContextLoader`.
`protected Class<? extends ContextLoader>`
`resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList)`
Resolve the `ContextLoader` class to use for the supplied list of `ContextConfigurationAttributes`.
`void`
`setBootstrapContext(BootstrapContext bootstrapContext)`
Set the `BootstrapContext` to be used by this bootstrapper.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AbstractTestContextBootstrapper

public AbstractTestContextBootstrapper()

## Method Details

### setBootstrapContext

public void setBootstrapContext(BootstrapContext bootstrapContext)
Description copied from interface: `TestContextBootstrapper`
Set the `BootstrapContext` to be used by this bootstrapper.

Specified by:
  `setBootstrapContext` in interface `TestContextBootstrapper`

### getBootstrapContext

public BootstrapContext getBootstrapContext()
Description copied from interface: `TestContextBootstrapper`
Get the `BootstrapContext` associated with this bootstrapper.

Specified by:
  `getBootstrapContext` in interface `TestContextBootstrapper`

### buildTestContext

public TestContext buildTestContext()
Build a new `DefaultTestContext` using the test class in the `BootstrapContext` associated with this bootstrapper and by delegating to `buildMergedContextConfiguration()` and `getCacheAwareContextLoaderDelegate()`.

Concrete subclasses may choose to override this method to return a custom `TestContext` implementation.

Specified by:
  `buildTestContext` in interface `TestContextBootstrapper`
Returns:
  a new `TestContext`, never `null`
Since:
  4.2
See Also:
  - `TestContextBootstrapper.buildMergedContextConfiguration()`

### getTestExecutionListeners

public final List<TestExecutionListener> getTestExecutionListeners()
Description copied from interface: `TestContextBootstrapper`
Get a list of newly instantiated `TestExecutionListeners` for the test class in the `BootstrapContext` associated with this bootstrapper.

If `@TestExecutionListeners` is not present on the test class in the `BootstrapContext`, default listeners should be returned. Furthermore, default listeners must be sorted using `AnnotationAwareOrderComparator`.

Concrete implementations are free to determine what comprises the set of default listeners. However, by default, the Spring TestContext Framework will use the `SpringFactoriesLoader` mechanism to look up all `TestExecutionListener` class names configured in all `META-INF/spring.factories` files on the classpath.

The `inheritListeners` flag of `@TestExecutionListeners` must be taken into consideration. Specifically, if the `inheritListeners` flag is set to `true`, listeners declared for a given test class must be appended to the end of the list of listeners declared in superclasses or enclosing classes.

Specified by:
  `getTestExecutionListeners` in interface `TestContextBootstrapper`
Returns:
  a list of `TestExecutionListener` instances

### getDefaultTestExecutionListeners

protected List<TestExecutionListener> getDefaultTestExecutionListeners()
Get the default `TestExecutionListeners` for this bootstrapper.

The default implementation delegates to `TestContextSpringFactoriesUtils.loadFactoryImplementations(Class)`.

This method is invoked by `getTestExecutionListeners()`.

Returns:
  an unmodifiable list of default `TestExecutionListener` instances
Since:
  6.0

### buildMergedContextConfiguration

public final MergedContextConfiguration buildMergedContextConfiguration()
Description copied from interface: `TestContextBootstrapper`
Build the merged context configuration for the test class in the `BootstrapContext` associated with this bootstrapper.

Implementations must take the following into account when building the merged configuration:

- Context hierarchies declared via `@ContextHierarchy` and `@ContextConfiguration`
- Active bean definition profiles declared via `@ActiveProfiles`
- Context initializers declared via `ContextConfiguration.initializers()`
- Test property sources declared via `@TestPropertySource`

Consult the Javadoc for the aforementioned annotations for details on the required semantics.

Note that the implementation of `TestContextBootstrapper.buildTestContext()` should typically delegate to this method when constructing the `TestContext`.

When determining which `ContextLoader` to use for a given test class, the following algorithm should be used:

1. If a `ContextLoader` class has been explicitly declared via `ContextConfiguration.loader()`, use it.
2. Otherwise, concrete implementations are free to determine which `ContextLoader` class to use as a default.

Specified by:
  `buildMergedContextConfiguration` in interface `TestContextBootstrapper`
Returns:
  the merged context configuration, never `null`
See Also:
  - `TestContextBootstrapper.buildTestContext()`

### getContextCustomizerFactories

protected List<ContextCustomizerFactory> getContextCustomizerFactories()
Get the `ContextCustomizerFactory` instances for this bootstrapper.

The default implementation delegates to `TestContextSpringFactoriesUtils.loadFactoryImplementations(Class)`.

Since:
  4.3

### resolveContextLoader

protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList)
Resolve the `ContextLoader` class to use for the supplied list of `ContextConfigurationAttributes` and then instantiate and return that `ContextLoader`.

If the user has not explicitly declared which loader to use, the value returned from `getDefaultContextLoaderClass(java.lang.Class<?>)` will be used as the default context loader class. For details on the class resolution process, see `resolveExplicitContextLoaderClass(java.util.List<org.springframework.test.context.ContextConfigurationAttributes>)` and `getDefaultContextLoaderClass(java.lang.Class<?>)`.

Parameters:
  `testClass` - the test class for which the `ContextLoader` should be resolved; must not be `null`
  `configAttributesList` - the list of configuration attributes to process; must not be `null`; must be ordered bottom-up (i.e., as if we were traversing up the class hierarchy and enclosing class hierarchy)
Returns:
  the resolved `ContextLoader` for the supplied `testClass` (never `null`)
Throws:
  `IllegalStateException` - if `getDefaultContextLoaderClass(Class)` returns `null`

### resolveExplicitContextLoaderClass

@Nullable protected Class<? extends ContextLoader> resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList)
Resolve the `ContextLoader` class to use for the supplied list of `ContextConfigurationAttributes`.

Beginning with the first level in the context configuration attributes hierarchy:

1. If the `contextLoaderClass` property of `ContextConfigurationAttributes` is configured with an explicit class, that class will be returned.
2. If an explicit `ContextLoader` class is not specified at the current level in the hierarchy, traverse to the next level in the hierarchy and return to step #1.

Parameters:
  `configAttributesList` - the list of configuration attributes to process; must not be `null`; must be ordered bottom-up (i.e., as if we were traversing up the class hierarchy and enclosing class hierarchy)
Returns:
  the `ContextLoader` class to use for the supplied configuration attributes, or `null` if no explicit loader is found
Throws:
  `IllegalArgumentException` - if supplied configuration attributes are `null` or empty

### getCacheAwareContextLoaderDelegate

protected CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate()
Get the `CacheAwareContextLoaderDelegate` to use for transparent interaction with the `ContextCache`.

The default implementation delegates to `getBootstrapContext().getCacheAwareContextLoaderDelegate()` and the default one will load `ApplicationContextFailureProcessor` via the service loading mechanism.

Concrete subclasses may choose to override this method to return a custom `CacheAwareContextLoaderDelegate` implementation with custom `ContextCache` support.

Returns:
  the context loader delegate (never `null`)
See Also:
  - `ApplicationContextFailureProcessor`

### getDefaultContextLoaderClass

protected abstract Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass)
Determine the default `ContextLoader` class to use for the supplied test class.

The class returned by this method will only be used if a `ContextLoader` class has not been explicitly declared via `ContextConfiguration.loader()`.

Parameters:
  `testClass` - the test class for which to retrieve the default `ContextLoader` class
Returns:
  the default `ContextLoader` class for the supplied test class (never `null`)

### processMergedContextConfiguration

protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Process the supplied, newly instantiated `MergedContextConfiguration` instance.

The returned `MergedContextConfiguration` instance may be a wrapper around or a replacement for the original.

The default implementation simply returns the supplied instance unmodified.

Concrete subclasses may choose to return a specialized subclass of `MergedContextConfiguration` based on properties in the supplied instance.

Parameters:
  `mergedConfig` - the `MergedContextConfiguration` to process; never `null`
Returns:
  a fully initialized `MergedContextConfiguration`; never `null`