public abstract class AbstractTestContextBootstrapper extends java.lang.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() |
Modifier and Type | Method and Description |
---|---|
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 java.util.List<ContextCustomizerFactory> |
getContextCustomizerFactories()
Get the
ContextCustomizerFactory instances for this bootstrapper. |
protected abstract java.lang.Class<? extends ContextLoader> |
getDefaultContextLoaderClass(java.lang.Class<?> testClass)
Determine the default
ContextLoader class
to use for the supplied test class. |
protected java.util.Set<java.lang.Class<? extends TestExecutionListener>> |
getDefaultTestExecutionListenerClasses()
Get the default
TestExecutionListener classes for this bootstrapper. |
protected java.util.List<java.lang.String> |
getDefaultTestExecutionListenerClassNames()
Get the names of the default
TestExecutionListener classes for
this bootstrapper. |
java.util.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(java.lang.Class<?> testClass,
java.util.List<ContextConfigurationAttributes> configAttributesList)
Resolve the
ContextLoader class to use for the
supplied list of ContextConfigurationAttributes and then instantiate
and return that ContextLoader . |
protected java.lang.Class<? extends ContextLoader> |
resolveExplicitContextLoaderClass(java.util.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. |
public void setBootstrapContext(BootstrapContext bootstrapContext)
BootstrapContext
to be used by this bootstrapper.setBootstrapContext
in interface TestContextBootstrapper
public BootstrapContext getBootstrapContext()
BootstrapContext
associated with this bootstrapper.getBootstrapContext
in interface TestContextBootstrapper
public 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 TestContextBootstrapper
TestContext
, never null
TestContextBootstrapper.buildMergedContextConfiguration()
public final java.util.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 TestContextBootstrapper
TestExecutionListener
instancesprotected java.util.Set<java.lang.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 java.util.List<java.lang.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
@ActiveProfiles
ContextConfiguration.initializers()
@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:
ContextLoader
class has been explicitly declared via
ContextConfiguration.loader()
, use it.ContextLoader
class to use as a default.buildMergedContextConfiguration
in interface TestContextBootstrapper
null
TestContextBootstrapper.buildTestContext()
protected java.util.List<ContextCustomizerFactory> getContextCustomizerFactories()
ContextCustomizerFactory
instances for this bootstrapper.
The default implementation uses the SpringFactoriesLoader
mechanism
for loading factories configured in all META-INF/spring.factories
files on the classpath.
SpringFactoriesLoader.loadFactories(java.lang.Class<T>, java.lang.ClassLoader)
protected ContextLoader resolveContextLoader(java.lang.Class<?> testClass, java.util.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 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)ContextLoader
for the supplied testClass
(never null
)java.lang.IllegalStateException
- if getDefaultContextLoaderClass(Class)
returns null
protected java.lang.Class<? extends ContextLoader> resolveExplicitContextLoaderClass(java.util.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
; 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 foundjava.lang.IllegalArgumentException
- 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 java.lang.Class<? extends ContextLoader> getDefaultContextLoaderClass(java.lang.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 null
MergedContextConfiguration
; never null