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
-
Method Summary
Modifier and TypeMethodDescriptionprotected Class<?>[]
detectDefaultConfigurationClasses
(Class<?> declaringClass) Detect the default configuration classes for the supplied test class.protected org.springframework.boot.ApplicationContextFactory
getApplicationContextFactory
(org.springframework.test.context.MergedContextConfiguration mergedConfig) Return theApplicationContextFactory
that should be used for the test.protected org.springframework.core.env.ConfigurableEnvironment
Returns theConfigurableEnvironment
instance that should be applied toSpringApplication
ornull
to use the default.protected List<org.springframework.context.ApplicationContextInitializer<?>>
getInitializers
(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.boot.SpringApplication application) Return theinitializers
that will be applied to the context.protected String[]
getInlinedProperties
(org.springframework.test.context.MergedContextConfiguration mergedConfig) protected String
protected String[]
protected org.springframework.boot.SpringApplication
Builds newSpringApplication
instance.org.springframework.context.ApplicationContext
loadContext
(org.springframework.test.context.MergedContextConfiguration mergedConfig) org.springframework.context.ApplicationContext
loadContextForAotProcessing
(org.springframework.test.context.MergedContextConfiguration mergedConfig) org.springframework.context.ApplicationContext
loadContextForAotRuntime
(org.springframework.test.context.MergedContextConfiguration mergedConfig, org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext> initializer) void
processContextConfiguration
(org.springframework.test.context.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
public org.springframework.context.ApplicationContext loadContext(org.springframework.test.context.MergedContextConfiguration mergedConfig) throws Exception - Specified by:
loadContext
in interfaceorg.springframework.test.context.SmartContextLoader
- Throws:
Exception
-
loadContextForAotProcessing
public org.springframework.context.ApplicationContext loadContextForAotProcessing(org.springframework.test.context.MergedContextConfiguration mergedConfig) throws Exception - Specified by:
loadContextForAotProcessing
in 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:
loadContextForAotRuntime
in interfaceorg.springframework.test.context.aot.AotContextLoader
- Throws:
Exception
-
getApplicationContextFactory
protected org.springframework.boot.ApplicationContextFactory getApplicationContextFactory(org.springframework.test.context.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
protected org.springframework.boot.SpringApplication getSpringApplication()Builds newSpringApplication
instance. This method is only called when amain
method isn't being used to create theSpringApplication
.- Returns:
- a
SpringApplication
instance
-
getEnvironment
protected org.springframework.core.env.ConfigurableEnvironment 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
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 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
public void processContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes) - Specified by:
processContextConfiguration
in interfaceorg.springframework.test.context.SmartContextLoader
- Overrides:
processContextConfiguration
in 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:
getResourceSuffixes
in classorg.springframework.test.context.support.AbstractContextLoader
-
getResourceSuffix
- Specified by:
getResourceSuffix
in classorg.springframework.test.context.support.AbstractContextLoader
-