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:
To plug in custom
ContextCache
support, override getCacheAwareContextLoaderDelegate().
- Since:
- 4.1
- Author:
- Sam Brannen, Juergen Hoeller, Phillip Webb
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionBuild the merged context configuration for the test class in theBootstrapContextassociated with this bootstrapper.Build a newDefaultTestContextusing the test class in theBootstrapContextassociated with this bootstrapper and by delegating tobuildMergedContextConfiguration()andgetCacheAwareContextLoaderDelegate().Get theBootstrapContextassociated with this bootstrapper.protected CacheAwareContextLoaderDelegateGet theCacheAwareContextLoaderDelegateto use for transparent interaction with theContextCache.protected List<ContextCustomizerFactory> Get theContextCustomizerFactoryinstances for this bootstrapper.protected abstract Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) Determine the defaultContextLoaderclass to use for the supplied test class.protected List<TestExecutionListener> Get the defaultTestExecutionListenersfor this bootstrapper.final List<TestExecutionListener> Get a list of newly instantiatedTestExecutionListenersfor the test class in theBootstrapContextassociated with this bootstrapper.protected MergedContextConfigurationprocessMergedContextConfiguration(MergedContextConfiguration mergedConfig) Process the supplied, newly instantiatedMergedContextConfigurationinstance.protected ContextLoaderresolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoaderclass to use for the supplied list ofContextConfigurationAttributesand then instantiate and return thatContextLoader.protected @Nullable Class<? extends ContextLoader> resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList) voidsetBootstrapContext(BootstrapContext bootstrapContext) Set theBootstrapContextto be used by this bootstrapper.
-
Constructor Details
-
AbstractTestContextBootstrapper
public AbstractTestContextBootstrapper()
-
-
Method Details
-
setBootstrapContext
Description copied from interface:TestContextBootstrapperSet theBootstrapContextto be used by this bootstrapper.- Specified by:
setBootstrapContextin interfaceTestContextBootstrapper
-
getBootstrapContext
Description copied from interface:TestContextBootstrapperGet theBootstrapContextassociated with this bootstrapper.- Specified by:
getBootstrapContextin interfaceTestContextBootstrapper
-
buildTestContext
Build a newDefaultTestContextusing the test class in theBootstrapContextassociated with this bootstrapper and by delegating tobuildMergedContextConfiguration()andgetCacheAwareContextLoaderDelegate().Concrete subclasses may choose to override this method to return a custom
TestContextimplementation.- Specified by:
buildTestContextin interfaceTestContextBootstrapper- Returns:
- a new
TestContext, nevernull - Since:
- 4.2
- See Also:
-
getTestExecutionListeners
Description copied from interface:TestContextBootstrapperGet a list of newly instantiatedTestExecutionListenersfor the test class in theBootstrapContextassociated with this bootstrapper.If
@TestExecutionListenersis 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
SpringFactoriesLoadermechanism to look up allTestExecutionListenerclass names configured in allMETA-INF/spring.factoriesfiles on the classpath.The
inheritListenersflag of@TestExecutionListenersmust be taken into consideration. Specifically, if theinheritListenersflag 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:
getTestExecutionListenersin interfaceTestContextBootstrapper- Returns:
- a list of
TestExecutionListenerinstances
-
getDefaultTestExecutionListeners
Get the defaultTestExecutionListenersfor this bootstrapper.The default implementation delegates to
TestContextSpringFactoriesUtils.loadFactoryImplementations(Class).This method is invoked by
getTestExecutionListeners().- Returns:
- an unmodifiable list of default
TestExecutionListenerinstances - Since:
- 6.0
-
buildMergedContextConfiguration
Description copied from interface:TestContextBootstrapperBuild the merged context configuration for the test class in theBootstrapContextassociated with this bootstrapper.Implementations must take the following into account when building the merged configuration:
- Context hierarchies declared via
@ContextHierarchyand@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
ContextLoaderto use for a given test class, the following algorithm should be used:- If a
ContextLoaderclass has been explicitly declared viaContextConfiguration.loader(), use it. - Otherwise, concrete implementations are free to determine which
ContextLoaderclass to use as a default.
- Specified by:
buildMergedContextConfigurationin interfaceTestContextBootstrapper- Returns:
- the merged context configuration, never
null - See Also:
- Context hierarchies declared via
-
getContextCustomizerFactories
Get theContextCustomizerFactoryinstances for this bootstrapper.The default implementation delegates to
TestContextSpringFactoriesUtils.loadFactoryImplementations(Class).- Since:
- 4.3
-
resolveContextLoader
protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoaderclass to use for the supplied list ofContextConfigurationAttributesand then instantiate and return thatContextLoader.If the user has not explicitly declared which loader to use, the value returned from
getDefaultContextLoaderClass(Class)will be used as the default context loader class. For details on the class resolution process, seeresolveExplicitContextLoaderClass(List)andgetDefaultContextLoaderClass(Class).- Parameters:
testClass- the test class for which theContextLoadershould be resolved; must not benullconfigAttributesList- 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
ContextLoaderfor the suppliedtestClass(nevernull) - Throws:
IllegalStateException- ifgetDefaultContextLoaderClass(Class)returnsnull
-
resolveExplicitContextLoaderClass
protected @Nullable Class<? extends ContextLoader> resolveExplicitContextLoaderClass(List<ContextConfigurationAttributes> configAttributesList) Resolve theContextLoaderclass to use for the supplied list ofContextConfigurationAttributes.Beginning with the first level in the context configuration attributes hierarchy:
- If the
contextLoaderClassproperty ofContextConfigurationAttributesis configured with an explicit class, that class will be returned. - If an explicit
ContextLoaderclass 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
ContextLoaderclass to use for the supplied configuration attributes, ornullif no explicit loader is found - Throws:
IllegalArgumentException- if supplied configuration attributes arenullor empty
- If the
-
getCacheAwareContextLoaderDelegate
Get theCacheAwareContextLoaderDelegateto use for transparent interaction with theContextCache.The default implementation delegates to
getBootstrapContext().getCacheAwareContextLoaderDelegate()and the default one will loadApplicationContextFailureProcessorvia the service loading mechanism.Concrete subclasses may choose to override this method to return a custom
CacheAwareContextLoaderDelegateimplementation with customContextCachesupport.- Returns:
- the context loader delegate (never
null) - See Also:
-
getDefaultContextLoaderClass
Determine the defaultContextLoaderclass to use for the supplied test class.The class returned by this method will only be used if a
ContextLoaderclass has not been explicitly declared viaContextConfiguration.loader().- Parameters:
testClass- the test class for which to retrieve the defaultContextLoaderclass- Returns:
- the default
ContextLoaderclass for the supplied test class (nevernull)
-
processMergedContextConfiguration
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) Process the supplied, newly instantiatedMergedContextConfigurationinstance.The returned
MergedContextConfigurationinstance 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
MergedContextConfigurationbased on properties in the supplied instance.- Parameters:
mergedConfig- theMergedContextConfigurationto process; nevernull- Returns:
- a fully initialized
MergedContextConfiguration; nevernull
-