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 theTestContextBootstrapperinterface 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
ContextCachesupport, overridegetCacheAwareContextLoaderDelegate().- Since:
- 4.1
- Author:
- Sam Brannen, Juergen Hoeller, Phillip Webb
Constructor Summary
Constructors Constructor Description AbstractTestContextBootstrapper()
Method Summary
Constructor Detail
AbstractTestContextBootstrapper
public AbstractTestContextBootstrapper()
Method Detail
setBootstrapContext
public void setBootstrapContext(BootstrapContext bootstrapContext)
Description copied from interface:TestContextBootstrapperSet theBootstrapContextto be used by this bootstrapper.- Specified by:
setBootstrapContextin interfaceTestContextBootstrapper
getBootstrapContext
public BootstrapContext getBootstrapContext()
Description copied from interface:TestContextBootstrapperGet theBootstrapContextassociated with this bootstrapper.- Specified by:
getBootstrapContextin interfaceTestContextBootstrapper
buildTestContext
public TestContext 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:
TestContextBootstrapper.buildMergedContextConfiguration()
getTestExecutionListeners
public final List<TestExecutionListener> 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.- Specified by:
getTestExecutionListenersin interfaceTestContextBootstrapper- Returns:
- a list of
TestExecutionListenerinstances
getDefaultTestExecutionListenerClasses
protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses()
Get the defaultTestExecutionListenerclasses for this bootstrapper.This method is invoked by
getTestExecutionListeners()and delegates togetDefaultTestExecutionListenerClassNames()to retrieve the class names.If a particular class cannot be loaded, a
DEBUGmessage will be logged, but the associated exception will not be rethrown.
getDefaultTestExecutionListenerClassNames
protected List<String> getDefaultTestExecutionListenerClassNames()
Get the names of the defaultTestExecutionListenerclasses for this bootstrapper.The default implementation looks up all
org.springframework.test.context.TestExecutionListenerentries configured in allMETA-INF/spring.factoriesfiles on the classpath.This method is invoked by
getDefaultTestExecutionListenerClasses().- Returns:
- an unmodifiable list of names of default
TestExecutionListenerclasses - See Also:
SpringFactoriesLoader.loadFactoryNames(java.lang.Class<?>, java.lang.ClassLoader)
buildMergedContextConfiguration
public final MergedContextConfiguration buildMergedContextConfiguration()
Build 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:
TestContextBootstrapper.buildTestContext()
- Context hierarchies declared via
getContextCustomizerFactories
protected List<ContextCustomizerFactory> getContextCustomizerFactories()
Get theContextCustomizerFactoryinstances for this bootstrapper.The default implementation uses the
SpringFactoriesLoadermechanism for loading factories configured in allMETA-INF/spring.factoriesfiles on the classpath.
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(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 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)- Returns:
- the resolved
ContextLoaderfor the suppliedtestClass(nevernull) - Throws:
IllegalStateException- ifgetDefaultContextLoaderClass(Class)returnsnull
resolveExplicitContextLoaderClass
protected 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)- 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
protected CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate()
Get theCacheAwareContextLoaderDelegateto 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
CacheAwareContextLoaderDelegateimplementation with customContextCachesupport.- Returns:
- the context loader delegate (never
null)
getDefaultContextLoaderClass
protected abstract Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass)
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