Search in sources :

Example 71 with SpringApplication

use of org.springframework.boot.SpringApplication 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 72 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class AnsiOutputApplicationListenerTests method enabled.

@Test
void enabled() {
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    Map<String, Object> props = new HashMap<>();
    props.put("spring.output.ansi.enabled", "ALWAYS");
    application.setDefaultProperties(props);
    this.context = application.run();
    assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.ALWAYS);
}
Also used : HashMap(java.util.HashMap) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.jupiter.api.Test)

Example 73 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class AnsiOutputApplicationListenerTests method disabled.

@Test
void disabled() {
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    Map<String, Object> props = new HashMap<>();
    props.put("spring.output.ansi.enabled", "never");
    application.setDefaultProperties(props);
    this.context = application.run();
    assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.NEVER);
}
Also used : HashMap(java.util.HashMap) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.jupiter.api.Test)

Example 74 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class ApplicationPidFileWriterTests method createPreparedEvent.

private SpringApplicationEvent createPreparedEvent(String propName, String propValue) {
    ConfigurableEnvironment environment = createEnvironment(propName, propValue);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    given(context.getEnvironment()).willReturn(environment);
    return new ApplicationPreparedEvent(new SpringApplication(), new String[] {}, context);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent)

Example 75 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests method setup.

@BeforeEach
void setup() {
    this.application = new SpringApplication(Config.class);
    this.application.setWebApplicationType(WebApplicationType.NONE);
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SpringApplication (org.springframework.boot.SpringApplication)205 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)56 Test (org.junit.Test)43 Test (org.junit.jupiter.api.Test)39 Environment (org.springframework.core.env.Environment)21 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)15 HashMap (java.util.HashMap)9 ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)9 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)9 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)9 File (java.io.File)7 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 JavaLoggingSystem (org.springframework.boot.logging.java.JavaLoggingSystem)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationPreparedEvent)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)4 JoranException (ch.qos.logback.core.joran.spi.JoranException)3 IOException (java.io.IOException)3