Add test for custom context initializer

[#48127729] [bs-22] Add missing unit tests
This commit is contained in:
Dave Syer 2013-04-26 11:05:14 +01:00
parent 4ca26058fa
commit ec351e5f7d

View File

@ -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<ApplicationContext> reference = new AtomicReference<ApplicationContext>();
application
.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
@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);