Class SpringBootContextLoader
java.lang.Object
org.springframework.test.context.support.AbstractContextLoader
org.springframework.boot.test.context.SpringBootContextLoader
- All Implemented Interfaces:
org.springframework.test.context.aot.AotContextLoader,org.springframework.test.context.ContextLoader,org.springframework.test.context.SmartContextLoader
public class SpringBootContextLoader
extends org.springframework.test.context.support.AbstractContextLoader
implements org.springframework.test.context.aot.AotContextLoader
A
ContextLoader that can be used to test Spring Boot applications (those that
normally startup using SpringApplication). Although this loader can be used
directly, most test will instead want to use it with
@SpringBootTest.
The loader supports both standard MergedContextConfiguration as well as
WebMergedContextConfiguration. If WebMergedContextConfiguration is used
the context will either use a mock servlet environment, or start the full embedded web
server.
If @ActiveProfiles are provided in the test class they will be used to create
the application context.
- Since:
- 1.4.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Class<?>[]detectDefaultConfigurationClasses(Class<?> declaringClass) Detect the default configuration classes for the supplied test class.protected org.springframework.boot.ApplicationContextFactorygetApplicationContextFactory(org.springframework.test.context.MergedContextConfiguration mergedConfig) Return theApplicationContextFactorythat should be used for the test.protected org.springframework.core.env.ConfigurableEnvironmentReturns theConfigurableEnvironmentinstance that should be applied toSpringApplicationornullto use the default.protected List<org.springframework.context.ApplicationContextInitializer<?>>getInitializers(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.boot.SpringApplication application) Return theinitializersthat will be applied to the context.protected String[]getInlinedProperties(org.springframework.test.context.MergedContextConfiguration mergedConfig) protected Stringprotected String[]protected org.springframework.boot.SpringApplicationBuilds newSpringApplicationinstance.org.springframework.context.ApplicationContextloadContext(org.springframework.test.context.MergedContextConfiguration mergedConfig) org.springframework.context.ApplicationContextloadContextForAotProcessing(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.aot.hint.RuntimeHints runtimeHints) org.springframework.context.ApplicationContextloadContextForAotRuntime(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext> initializer) voidprocessContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes) Methods inherited from class org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, isGenerateDefaultLocations, modifyLocations, prepareContext, processLocationsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.test.context.aot.AotContextLoader
loadContextForAotProcessingMethods inherited from interface org.springframework.test.context.SmartContextLoader
loadContext, processLocations
-
Constructor Details
-
SpringBootContextLoader
public SpringBootContextLoader()
-
-
Method Details
-
loadContext
public org.springframework.context.ApplicationContext loadContext(org.springframework.test.context.MergedContextConfiguration mergedConfig) throws Exception - Specified by:
loadContextin interfaceorg.springframework.test.context.SmartContextLoader- Throws:
Exception
-
loadContextForAotProcessing
public org.springframework.context.ApplicationContext loadContextForAotProcessing(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.aot.hint.RuntimeHints runtimeHints) throws Exception - Specified by:
loadContextForAotProcessingin interfaceorg.springframework.test.context.aot.AotContextLoader- Throws:
Exception
-
loadContextForAotRuntime
public org.springframework.context.ApplicationContext loadContextForAotRuntime(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext> initializer) throws Exception - Specified by:
loadContextForAotRuntimein interfaceorg.springframework.test.context.aot.AotContextLoader- Throws:
Exception
-
getApplicationContextFactory
protected org.springframework.boot.ApplicationContextFactory getApplicationContextFactory(org.springframework.test.context.MergedContextConfiguration mergedConfig) Return theApplicationContextFactorythat should be used for the test. By default this method will return a factory that will create an appropriateApplicationContextfor theWebApplicationType.- Parameters:
mergedConfig- the merged context configuration- Returns:
- the application context factory to use
- Since:
- 3.2.0
-
getSpringApplication
protected org.springframework.boot.SpringApplication getSpringApplication()Builds newSpringApplicationinstance. This method is only called when amainmethod isn't being used to create theSpringApplication.- Returns:
- a
SpringApplicationinstance
-
getEnvironment
protected org.springframework.core.env.ConfigurableEnvironment getEnvironment()Returns theConfigurableEnvironmentinstance that should be applied toSpringApplicationornullto use the default. You can override this method if you need a custom environment.- Returns:
- a
ConfigurableEnvironmentinstance
-
getInlinedProperties
protected String[] getInlinedProperties(org.springframework.test.context.MergedContextConfiguration mergedConfig) -
getInitializers
protected List<org.springframework.context.ApplicationContextInitializer<?>> getInitializers(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.boot.SpringApplication application) Return theinitializersthat will be applied to the context. By default this method will adaptcontext customizers, addapplication initializersand addinitializers specified on the test.- Parameters:
mergedConfig- the source context configurationapplication- the application instance- Returns:
- the initializers to apply
- Since:
- 2.0.0
-
processContextConfiguration
public void processContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes) - Specified by:
processContextConfigurationin interfaceorg.springframework.test.context.SmartContextLoader- Overrides:
processContextConfigurationin classorg.springframework.test.context.support.AbstractContextLoader
-
detectDefaultConfigurationClasses
Detect the default configuration classes for the supplied test class. By default simply delegates toAnnotationConfigContextLoaderUtils.detectDefaultConfigurationClasses(java.lang.Class<?>).- Parameters:
declaringClass- the test class that declared@ContextConfiguration- Returns:
- an array of default configuration classes, potentially empty but never
null - See Also:
-
AnnotationConfigContextLoaderUtils
-
getResourceSuffixes
- Overrides:
getResourceSuffixesin classorg.springframework.test.context.support.AbstractContextLoader
-
getResourceSuffix
- Specified by:
getResourceSuffixin classorg.springframework.test.context.support.AbstractContextLoader
-