diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java index 372ffd1b48..84bc487703 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java @@ -16,6 +16,8 @@ package org.springframework.bootstrap; +import java.util.concurrent.atomic.AtomicReference; + import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -28,6 +30,7 @@ import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWe import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigUtils; @@ -130,6 +133,24 @@ public class SpringApplicationTests { assertThat(this.context, instanceOf(StaticApplicationContext.class)); } + @Test + public void specificApplicationContextInitializer() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + final AtomicReference reference = new AtomicReference(); + application + .addInitializers(new ApplicationContextInitializer() { + @Override + public void initialize(ConfigurableApplicationContext context) { + reference.set(context); + } + }); + this.context = application.run("--foo=bar"); + assertThat(this.context, sameInstance(reference.get())); + // Custom initializers do not switch off the defaults + assertThat(getEnvironment().getProperty("foo"), equalTo("bar")); + } + @Test public void defaultApplicationContext() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class);