Class SpringBootContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractContextLoader
- org.springframework.boot.test.context.SpringBootContextLoader
- All Implemented Interfaces:
org.springframework.test.context.ContextLoader,org.springframework.test.context.SmartContextLoader
public class SpringBootContextLoader extends org.springframework.test.context.support.AbstractContextLoader
AContextLoaderthat can be used to test Spring Boot applications (those that normally startup usingSpringApplication). Although this loader can be used directly, most test will instead want to use it withSpringBootTest.The loader supports both standard
MergedContextConfigurationas well asWebMergedContextConfiguration. IfWebMergedContextConfigurationis used the context will either use a mock servlet environment, or start the full embedded web server.If
@ActiveProfilesare provided in the test class they will be used to create the application context.- See Also:
SpringBootTest
Constructor Summary
Constructors Constructor Description SpringBootContextLoader()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Class<?>[]detectDefaultConfigurationClasses(Class<?> declaringClass)Detect the default configuration classes for the supplied test class.protected org.springframework.core.env.ConfigurableEnvironmentgetEnvironment()Builds a newConfigurableEnvironmentinstance.protected List<org.springframework.context.ApplicationContextInitializer<?>>getInitializers(org.springframework.test.context.MergedContextConfiguration config, org.springframework.boot.SpringApplication application)Return theinitializersthat will be applied to the context.protected String[]getInlinedProperties(org.springframework.test.context.MergedContextConfiguration config)protected StringgetResourceSuffix()protected String[]getResourceSuffixes()protected org.springframework.boot.SpringApplicationgetSpringApplication()Builds newSpringApplicationinstance.org.springframework.context.ApplicationContextloadContext(String... locations)org.springframework.context.ApplicationContextloadContext(org.springframework.test.context.MergedContextConfiguration config)voidprocessContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes)
Constructor Detail
SpringBootContextLoader
public SpringBootContextLoader()
Method Detail
loadContext
public org.springframework.context.ApplicationContext loadContext(org.springframework.test.context.MergedContextConfiguration config) throws Exception
- Throws:
Exception
getSpringApplication
protected org.springframework.boot.SpringApplication getSpringApplication()
Builds newSpringApplicationinstance. You can override this method to add custom behavior- Returns:
SpringApplicationinstance
getEnvironment
protected org.springframework.core.env.ConfigurableEnvironment getEnvironment()
Builds a newConfigurableEnvironmentinstance. You can override this method to return something other thanStandardEnvironmentif necessary.- Returns:
- a
ConfigurableEnvironmentinstance
getInlinedProperties
protected String[] getInlinedProperties(org.springframework.test.context.MergedContextConfiguration config)
getInitializers
protected List<org.springframework.context.ApplicationContextInitializer<?>> getInitializers(org.springframework.test.context.MergedContextConfiguration config, 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:
config- 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
protected Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass)
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
loadContext
public org.springframework.context.ApplicationContext loadContext(String... locations) throws Exception
- Throws:
Exception
getResourceSuffixes
protected String[] getResourceSuffixes()
- Overrides:
getResourceSuffixesin classorg.springframework.test.context.support.AbstractContextLoader
getResourceSuffix
protected String getResourceSuffix()
- Specified by:
getResourceSuffixin classorg.springframework.test.context.support.AbstractContextLoader