Class SpringBootContextLoader

  • All Implemented Interfaces:
    org.springframework.test.context.ContextLoader, org.springframework.test.context.SmartContextLoader

    public class SpringBootContextLoader
    extends org.springframework.test.context.support.AbstractContextLoader
    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.

    See Also:
    SpringBootTest
    • 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 new SpringApplication instance. You can override this method to add custom behavior
        Returns:
        SpringApplication instance
      • getEnvironment

        protected org.springframework.core.env.ConfigurableEnvironment getEnvironment()
        Builds a new ConfigurableEnvironment instance. You can override this method to return something other than StandardEnvironment if necessary.
        Returns:
        a ConfigurableEnvironment instance
      • 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 the initializers that will be applied to the context. By default this method will adapt context customizers, add application initializers and add initializers specified on the test.
        Parameters:
        config - the source context configuration
        application - 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 interface org.springframework.test.context.SmartContextLoader
        Overrides:
        processContextConfiguration in class org.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 to AnnotationConfigContextLoaderUtils.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

        protected String[] getResourceSuffixes()
        Overrides:
        getResourceSuffixes in class org.springframework.test.context.support.AbstractContextLoader
      • getResourceSuffix

        protected String getResourceSuffix()
        Specified by:
        getResourceSuffix in class org.springframework.test.context.support.AbstractContextLoader