public abstract class AbstractTestContextBootstrapper extends Object implements TestContextBootstrapper
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().
| Constructor and Description |
|---|
AbstractTestContextBootstrapper() |
public void setBootstrapContext(BootstrapContext bootstrapContext)
BootstrapContext to be used by this bootstrapper.setBootstrapContext in interface TestContextBootstrapperpublic BootstrapContext getBootstrapContext()
BootstrapContext associated with this bootstrapper.getBootstrapContext in interface TestContextBootstrapperpublic TestContext buildTestContext()
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.
buildTestContext in interface TestContextBootstrapperTestContext, never nullTestContextBootstrapper.buildMergedContextConfiguration()public final List<TestExecutionListener> getTestExecutionListeners()
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.
getTestExecutionListeners in interface TestContextBootstrapperTestExecutionListener instancesprotected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses()
TestExecutionListener classes for this bootstrapper.
This method is invoked by getTestExecutionListeners() and
delegates to getDefaultTestExecutionListenerClassNames() to
retrieve the class names.
If a particular class cannot be loaded, a DEBUG message will
be logged, but the associated exception will not be rethrown.
protected List<String> getDefaultTestExecutionListenerClassNames()
TestExecutionListener classes for
this bootstrapper.
The default implementation looks up all
org.springframework.test.context.TestExecutionListener entries
configured in all META-INF/spring.factories files on the classpath.
This method is invoked by getDefaultTestExecutionListenerClasses().
TestExecutionListener
classesSpringFactoriesLoader.loadFactoryNames(java.lang.Class<?>, java.lang.ClassLoader)public final MergedContextConfiguration buildMergedContextConfiguration()
BootstrapContext associated with this
bootstrapper.
Implementations must take the following into account when building the merged configuration:
@ContextHierarchy
and @ContextConfiguration@ActiveProfilesContextConfiguration.initializers()@TestPropertySourceConsult 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:
ContextLoader class has been explicitly declared via
ContextConfiguration.loader(), use it.ContextLoader class to use as a default.buildMergedContextConfiguration in interface TestContextBootstrappernullTestContextBootstrapper.buildTestContext()protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList)
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<?>).
testClass - the test class for which the ContextLoader should be
resolved; must not be nullconfigAttributesList - the list of configuration attributes to process; must
not be null or empty; must be ordered bottom-up
(i.e., as if we were traversing up the class hierarchy)ContextLoader for the supplied testClass
(never null)IllegalStateException - if getDefaultContextLoaderClass(Class)
returns nullprotected Class<? extends ContextLoader> resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList)
ContextLoader class to use for the supplied
list of ContextConfigurationAttributes.
Beginning with the first level in the context configuration attributes hierarchy:
contextLoaderClass property of ContextConfigurationAttributes is
configured with an explicit class, that class will be returned.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.configAttributesList - the list of configuration attributes to process;
must not be null or empty; must be ordered bottom-up
(i.e., as if we were traversing up the class hierarchy)ContextLoader class to use for the supplied configuration
attributes, or null if no explicit loader is foundIllegalArgumentException - if supplied configuration attributes are
null or emptyprotected CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate()
CacheAwareContextLoaderDelegate to use for transparent
interaction with the ContextCache.
The default implementation simply delegates to
getBootstrapContext().getCacheAwareContextLoaderDelegate().
Concrete subclasses may choose to override this method to return a custom
CacheAwareContextLoaderDelegate implementation with custom
ContextCache support.
null)protected abstract Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass)
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().
testClass - the test class for which to retrieve the default
ContextLoader classContextLoader class for the supplied test class
(never null)protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig)
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.
mergedConfig - the MergedContextConfiguration to process; never nullMergedContextConfiguration; never null