Class SpringBootContextLoader
java.lang.Object
org.springframework.test.context.support.AbstractContextLoader
org.springframework.boot.test.context.SpringBootContextLoader
- All Implemented Interfaces:
AotContextLoader
,ContextLoader
,SmartContextLoader
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
- Author:
- Dave Syer, Phillip Webb, Andy Wilkinson, Stephane Nicoll, Madhura Bhave, Scott Frederick
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Class<?>[]
detectDefaultConfigurationClasses
(Class<?> declaringClass) Detect the default configuration classes for the supplied test class.protected ApplicationContextFactory
getApplicationContextFactory
(MergedContextConfiguration mergedConfig) Return theApplicationContextFactory
that should be used for the test.protected ConfigurableEnvironment
Returns theConfigurableEnvironment
instance that should be applied toSpringApplication
ornull
to use the default.protected List<ApplicationContextInitializer<?>>
getInitializers
(MergedContextConfiguration mergedConfig, SpringApplication application) Return theinitializers
that will be applied to the context.protected String[]
getInlinedProperties
(MergedContextConfiguration mergedConfig) protected String
protected String[]
protected SpringApplication
Builds newSpringApplication
instance.loadContext
(MergedContextConfiguration mergedConfig) loadContextForAotProcessing
(MergedContextConfiguration mergedConfig) loadContextForAotRuntime
(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) void
processContextConfiguration
(ContextConfigurationAttributes configAttributes) Methods inherited from class org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, isGenerateDefaultLocations, modifyLocations, prepareContext, processLocations
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.test.context.SmartContextLoader
loadContext, processLocations
-
Constructor Details
-
SpringBootContextLoader
public SpringBootContextLoader()
-
-
Method Details
-
loadContext
- Specified by:
loadContext
in interfaceSmartContextLoader
- Throws:
Exception
-
loadContextForAotProcessing
public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception - Specified by:
loadContextForAotProcessing
in interfaceAotContextLoader
- Throws:
Exception
-
loadContextForAotRuntime
public ApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception - Specified by:
loadContextForAotRuntime
in interfaceAotContextLoader
- Throws:
Exception
-
getApplicationContextFactory
protected ApplicationContextFactory getApplicationContextFactory(MergedContextConfiguration mergedConfig) Return theApplicationContextFactory
that should be used for the test. By default this method will return a factory that will create an appropriateApplicationContext
for theWebApplicationType
.- Parameters:
mergedConfig
- the merged context configuration- Returns:
- the application context factory to use
- Since:
- 3.2.0
-
getSpringApplication
Builds newSpringApplication
instance. This method is only called when amain
method isn't being used to create theSpringApplication
.- Returns:
- a
SpringApplication
instance
-
getEnvironment
Returns theConfigurableEnvironment
instance that should be applied toSpringApplication
ornull
to use the default. You can override this method if you need a custom environment.- Returns:
- a
ConfigurableEnvironment
instance
-
getInlinedProperties
-
getInitializers
protected List<ApplicationContextInitializer<?>> getInitializers(MergedContextConfiguration mergedConfig, SpringApplication application) Return theinitializers
that will be applied to the context. By default this method will adaptcontext customizers
, addapplication initializers
and addinitializers specified on the test
.- Parameters:
mergedConfig
- the source context configurationapplication
- the application instance- Returns:
- the initializers to apply
- Since:
- 2.0.0
-
processContextConfiguration
- Specified by:
processContextConfiguration
in interfaceSmartContextLoader
- Overrides:
processContextConfiguration
in classAbstractContextLoader
-
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:
-
getResourceSuffixes
- Overrides:
getResourceSuffixes
in classAbstractContextLoader
-
getResourceSuffix
- Specified by:
getResourceSuffix
in classAbstractContextLoader
-