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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Class<?>[]detectDefaultConfigurationClasses(Class<?> declaringClass) Detect the default configuration classes for the supplied test class.protected ConfigurableEnvironmentReturns theConfigurableEnvironmentinstance that should be applied toSpringApplicationornullto use the default.protected List<ApplicationContextInitializer<?>>getInitializers(MergedContextConfiguration mergedConfig, SpringApplication application) Return theinitializersthat will be applied to the context.protected String[]getInlinedProperties(MergedContextConfiguration mergedConfig) protected Stringprotected String[]protected SpringApplicationBuilds newSpringApplicationinstance.loadContext(MergedContextConfiguration mergedConfig) loadContextForAotProcessing(MergedContextConfiguration mergedConfig) loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) voidprocessContextConfiguration(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.SmartContextLoader
loadContext, processLocations
-
Constructor Details
-
SpringBootContextLoader
public SpringBootContextLoader()
-
-
Method Details
-
loadContext
- Specified by:
loadContextin interfaceSmartContextLoader- Throws:
Exception
-
loadContextForAotProcessing
public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception - Specified by:
loadContextForAotProcessingin interfaceAotContextLoader- Throws:
Exception
-
loadContextForAotRuntime
public ApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception - Specified by:
loadContextForAotRuntimein interfaceAotContextLoader- Throws:
Exception
-
getSpringApplication
Builds newSpringApplicationinstance. This method is only called when amainmethod isn't being used to create theSpringApplication.- Returns:
- a
SpringApplicationinstance
-
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
-
getInitializers
protected List<ApplicationContextInitializer<?>> getInitializers(MergedContextConfiguration mergedConfig, 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
- Specified by:
processContextConfigurationin interfaceSmartContextLoader- Overrides:
processContextConfigurationin 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:
getResourceSuffixesin classAbstractContextLoader
-
getResourceSuffix
- Specified by:
getResourceSuffixin classAbstractContextLoader
-