Search in sources :

Example 1 with WebMergedContextConfiguration

use of org.springframework.test.context.web.WebMergedContextConfiguration in project spring-boot by spring-projects.

the class SpringBootContextLoader method loadContext.

@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
    Class<?>[] configClasses = config.getClasses();
    String[] configLocations = config.getLocations();
    Assert.state(!ObjectUtils.isEmpty(configClasses) || !ObjectUtils.isEmpty(configLocations), () -> "No configuration classes or locations found in @SpringApplicationConfiguration. " + "For default configuration detection to work you need Spring 4.0.3 or better (found " + SpringVersion.getVersion() + ").");
    SpringApplication application = getSpringApplication();
    application.setMainApplicationClass(config.getTestClass());
    application.addPrimarySources(Arrays.asList(configClasses));
    application.getSources().addAll(Arrays.asList(configLocations));
    List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application);
    if (config instanceof WebMergedContextConfiguration) {
        application.setWebApplicationType(WebApplicationType.SERVLET);
        if (!isEmbeddedWebEnvironment(config)) {
            new WebConfigurer().configure(config, application, initializers);
        }
    } else if (config instanceof ReactiveWebMergedContextConfiguration) {
        application.setWebApplicationType(WebApplicationType.REACTIVE);
        if (!isEmbeddedWebEnvironment(config)) {
            application.setApplicationContextFactory(ApplicationContextFactory.of(GenericReactiveWebApplicationContext::new));
        }
    } else {
        application.setWebApplicationType(WebApplicationType.NONE);
    }
    application.setInitializers(initializers);
    ConfigurableEnvironment environment = getEnvironment();
    if (environment != null) {
        prepareEnvironment(config, application, environment, false);
        application.setEnvironment(environment);
    } else {
        application.addListeners(new PrepareEnvironmentListener(config));
    }
    String[] args = SpringBootTestArgs.get(config.getContextCustomizers());
    return application.run(args);
}
Also used : GenericReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ServletContextApplicationContextInitializer(org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer)

Example 2 with WebMergedContextConfiguration

use of org.springframework.test.context.web.WebMergedContextConfiguration in project spring-boot by spring-projects.

the class SpringBootTestContextBootstrapper method processMergedContextConfiguration.

@Override
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
    Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
    List<String> propertySourceProperties = getAndProcessPropertySourceProperties(mergedConfig);
    mergedConfig = createModifiedConfig(mergedConfig, classes, StringUtils.toStringArray(propertySourceProperties));
    WebEnvironment webEnvironment = getWebEnvironment(mergedConfig.getTestClass());
    if (webEnvironment != null && isWebEnvironmentSupported(mergedConfig)) {
        WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);
        if (webApplicationType == WebApplicationType.SERVLET && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
        } else if (webApplicationType == WebApplicationType.REACTIVE && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            return new ReactiveWebMergedContextConfiguration(mergedConfig);
        }
    }
    return mergedConfig;
}
Also used : WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) WebApplicationType(org.springframework.boot.WebApplicationType)

Example 3 with WebMergedContextConfiguration

use of org.springframework.test.context.web.WebMergedContextConfiguration in project spring-framework by spring-projects.

the class BootstrapTestUtilsMergedConfigTests method buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass.

/**
 * Introduced to investigate claims made in a discussion on
 * <a href="https://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
    Class<?> webTestClass = WebClassesFoo.class;
    Class<?> standardTestClass = ClassesFoo.class;
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
    MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);
    assertThat(webMergedConfig).isEqualTo(webMergedConfig);
    assertThat(standardMergedConfig).isEqualTo(standardMergedConfig);
    assertThat(webMergedConfig).isNotEqualTo(standardMergedConfig);
    assertThat(standardMergedConfig).isNotEqualTo(webMergedConfig);
    assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), WebDelegatingSmartContextLoader.class);
    assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), DelegatingSmartContextLoader.class);
}
Also used : WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

WebMergedContextConfiguration (org.springframework.test.context.web.WebMergedContextConfiguration)3 Test (org.junit.jupiter.api.Test)1 SpringApplication (org.springframework.boot.SpringApplication)1 WebApplicationType (org.springframework.boot.WebApplicationType)1 WebEnvironment (org.springframework.boot.test.context.SpringBootTest.WebEnvironment)1 GenericReactiveWebApplicationContext (org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext)1 ServletContextApplicationContextInitializer (org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer)1 ApplicationContextInitializer (org.springframework.context.ApplicationContextInitializer)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MergedContextConfiguration (org.springframework.test.context.MergedContextConfiguration)1