Search in sources :

Example 96 with SpringApplication

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

the class SpringBootDependencyInjectionTestExecutionListenerTests method prepareFailingTestInstanceShouldPrintReport.

@Test
public void prepareFailingTestInstanceShouldPrintReport() throws Exception {
    TestContext testContext = mock(TestContext.class);
    given(testContext.getTestInstance()).willThrow(new IllegalStateException());
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    ConfigurableApplicationContext applicationContext = application.run();
    given(testContext.getApplicationContext()).willReturn(applicationContext);
    try {
        this.reportListener.prepareTestInstance(testContext);
    } catch (IllegalStateException ex) {
    // Expected
    }
    this.out.expect(containsString("AUTO-CONFIGURATION REPORT"));
    this.out.expect(containsString("Positive matches"));
    this.out.expect(containsString("Negative matches"));
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 97 with SpringApplication

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

the class ConfigFileApplicationListenerTests method customDefaultProfile.

@Test
public void customDefaultProfile() throws Exception {
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    this.context = application.run("--spring.profiles.default=customdefault");
    String property = this.context.getEnvironment().getProperty("customdefault");
    assertThat(property).isEqualTo("true");
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.Test)

Example 98 with SpringApplication

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

the class ConfigFileApplicationListenerTests method profileCanBeIncludedWithoutAnyBeingActive.

@Test
public void profileCanBeIncludedWithoutAnyBeingActive() throws Exception {
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    this.context = application.run("--spring.profiles.include=dev");
    String property = this.context.getEnvironment().getProperty("my.property");
    assertThat(property).isEqualTo("fromdevpropertiesfile");
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.Test)

Example 99 with SpringApplication

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

the class ConfigFileApplicationListenerTests method validateProfilePrecedence.

private void validateProfilePrecedence(String... profiles) {
    ApplicationPreparedEvent event = new ApplicationPreparedEvent(new SpringApplication(), new String[0], new AnnotationConfigApplicationContext());
    this.initializer.onApplicationEvent(event);
    String log = this.out.toString();
    // First make sure that each profile got processed only once
    for (String profile : profiles) {
        String reason = "Wrong number of occurrences for profile '" + profile + "' --> " + log;
        assertThat(StringUtils.countOccurrencesOf(log, createLogForProfile(profile))).as(reason).isEqualTo(1);
    }
    // Make sure the order of loading is the right one
    for (String profile : profiles) {
        String line = createLogForProfile(profile);
        int index = log.indexOf(line);
        assertThat(index).as("Loading profile '" + profile + "' not found in '" + log + "'").isNotEqualTo(-1);
        log = log.substring(index + line.length());
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent)

Example 100 with SpringApplication

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

the class ConfigFileApplicationListenerTests method customDefaultProfileAndActiveFromFile.

@Test
public void customDefaultProfileAndActiveFromFile() throws Exception {
    // gh-5998
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    this.context = application.run("--spring.config.name=customprofile", "--spring.profiles.default=customdefault");
    ConfigurableEnvironment environment = this.context.getEnvironment();
    assertThat(environment.containsProperty("customprofile")).isTrue();
    assertThat(environment.containsProperty("customprofile-specific")).isTrue();
    assertThat(environment.containsProperty("customprofile-customdefault")).isTrue();
    assertThat(environment.acceptsProfiles("customdefault")).isTrue();
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.Test)

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