Class AbstractTestContextBootstrapper
- All Implemented Interfaces:
TestContextBootstrapper
- Direct Known Subclasses:
DefaultTestContextBootstrapper
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
-
Method Summary
Modifier and TypeMethodDescriptionBuild the merged context configuration for the test class in theBootstrapContext
associated with this bootstrapper.Build a newDefaultTestContext
using the test class in theBootstrapContext
associated with this bootstrapper and by delegating tobuildMergedContextConfiguration()
andgetCacheAwareContextLoaderDelegate()
.Get theBootstrapContext
associated with this bootstrapper.protected CacheAwareContextLoaderDelegate
Get theCacheAwareContextLoaderDelegate
to use for transparent interaction with theContextCache
.protected List<ContextCustomizerFactory>
Get theContextCustomizerFactory
instances for this bootstrapper.protected abstract Class<? extends ContextLoader>
getDefaultContextLoaderClass
(Class<?> testClass) Determine the defaultContextLoader
class to use for the supplied test class.protected List<TestExecutionListener>
Get the defaultTestExecutionListeners
for this bootstrapper.final List<TestExecutionListener>
Get a list of newly instantiatedTestExecutionListeners
for the test class in theBootstrapContext
associated with this bootstrapper.protected MergedContextConfiguration
processMergedContextConfiguration
(MergedContextConfiguration mergedConfig) Process the supplied, newly instantiatedMergedContextConfiguration
instance.protected ContextLoader
resolveContextLoader
(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoader
class to use for the supplied list ofContextConfigurationAttributes
and then instantiate and return thatContextLoader
.protected Class<? extends ContextLoader>
resolveExplicitContextLoaderClass
(List<ContextConfigurationAttributes> configAttributesList) void
setBootstrapContext
(BootstrapContext bootstrapContext) Set theBootstrapContext
to be used by this bootstrapper.
-
Constructor Details
-
AbstractTestContextBootstrapper
public AbstractTestContextBootstrapper()
-
-
Method Details
-
setBootstrapContext
Description copied from interface:TestContextBootstrapper
Set theBootstrapContext
to be used by this bootstrapper.- Specified by:
setBootstrapContext
in interfaceTestContextBootstrapper
-
getBootstrapContext
Description copied from interface:TestContextBootstrapper
Get theBootstrapContext
associated with this bootstrapper.- Specified by:
getBootstrapContext
in interfaceTestContextBootstrapper
-
buildTestContext
Build a newDefaultTestContext
using the test class in theBootstrapContext
associated with this bootstrapper and by delegating tobuildMergedContextConfiguration()
andgetCacheAwareContextLoaderDelegate()
.Concrete subclasses may choose to override this method to return a custom
TestContext
implementation.- Specified by:
buildTestContext
in interfaceTestContextBootstrapper
- Returns:
- a new
TestContext
, nevernull
- Since:
- 4.2
- See Also:
-
getTestExecutionListeners
Description copied from interface:TestContextBootstrapper
Get a list of newly instantiatedTestExecutionListeners
for the test class in theBootstrapContext
associated with this bootstrapper.If
@TestExecutionListeners
is not present on the test class in theBootstrapContext
, default listeners should be returned. Furthermore, default listeners must be sorted usingAnnotationAwareOrderComparator
.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 allTestExecutionListener
class names configured in allMETA-INF/spring.factories
files on the classpath.The
inheritListeners
flag of@TestExecutionListeners
must be taken into consideration. Specifically, if theinheritListeners
flag is set totrue
, 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 interfaceTestContextBootstrapper
- Returns:
- a list of
TestExecutionListener
instances
-
getDefaultTestExecutionListeners
Get the defaultTestExecutionListeners
for this bootstrapper.This method is invoked by
getTestExecutionListeners()
.The default implementation looks up and instantiates all
org.springframework.test.context.TestExecutionListener
entries configured in allMETA-INF/spring.factories
files on the classpath.If a particular listener cannot be loaded due to a
LinkageError
orClassNotFoundException
, aDEBUG
message will be logged, but the associated exception will not be rethrown. ARuntimeException
or any otherError
will be rethrown. Any other exception will be thrown wrapped in anIllegalStateException
.- Returns:
- an unmodifiable list of default
TestExecutionListener
instances - Since:
- 6.0
- See Also:
-
buildMergedContextConfiguration
Build the merged context configuration for the test class in theBootstrapContext
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 theTestContext
.When determining which
ContextLoader
to use for a given test class, the following algorithm should be used:- If a
ContextLoader
class has been explicitly declared viaContextConfiguration.loader()
, use it. - Otherwise, concrete implementations are free to determine which
ContextLoader
class to use as a default.
- Specified by:
buildMergedContextConfiguration
in interfaceTestContextBootstrapper
- Returns:
- the merged context configuration, never
null
- See Also:
- Context hierarchies declared via
-
getContextCustomizerFactories
Get theContextCustomizerFactory
instances for this bootstrapper.The default implementation uses the
SpringFactoriesLoader
mechanism for loading factories configured in allMETA-INF/spring.factories
files on the classpath.- Since:
- 4.3
- See Also:
-
resolveContextLoader
protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoader
class to use for the supplied list ofContextConfigurationAttributes
and then instantiate and return thatContextLoader
.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, seeresolveExplicitContextLoaderClass(java.util.List<org.springframework.test.context.ContextConfigurationAttributes>)
andgetDefaultContextLoaderClass(java.lang.Class<?>)
.- Parameters:
testClass
- the test class for which theContextLoader
should be resolved; must not benull
configAttributesList
- the list of configuration attributes to process; must not benull
; 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 suppliedtestClass
(nevernull
) - Throws:
IllegalStateException
- ifgetDefaultContextLoaderClass(Class)
returnsnull
-
resolveExplicitContextLoaderClass
@Nullable protected Class<? extends ContextLoader> resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoader
class to use for the supplied list ofContextConfigurationAttributes
.Beginning with the first level in the context configuration attributes hierarchy:
- If the
contextLoaderClass
property ofContextConfigurationAttributes
is configured with an explicit class, that class will be returned. - 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 benull
; 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, ornull
if no explicit loader is found - Throws:
IllegalArgumentException
- if supplied configuration attributes arenull
or empty
- If the
-
getCacheAwareContextLoaderDelegate
Get theCacheAwareContextLoaderDelegate
to use for transparent interaction with theContextCache
.The default implementation simply delegates to
getBootstrapContext().getCacheAwareContextLoaderDelegate()
.Concrete subclasses may choose to override this method to return a custom
CacheAwareContextLoaderDelegate
implementation with customContextCache
support.- Returns:
- the context loader delegate (never
null
)
-
getDefaultContextLoaderClass
Determine the defaultContextLoader
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 viaContextConfiguration.loader()
.- Parameters:
testClass
- the test class for which to retrieve the defaultContextLoader
class- Returns:
- the default
ContextLoader
class for the supplied test class (nevernull
)
-
processMergedContextConfiguration
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) Process the supplied, newly instantiatedMergedContextConfiguration
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
- theMergedContextConfiguration
to process; nevernull
- Returns:
- a fully initialized
MergedContextConfiguration
; nevernull
-