Provide a way to create custom ApplicationContextFactory

Update `SpringBootContextLoader` so that `getApplicationContextFactory`
is now a protected that may be overridden to provide a custom
`ApplicationContextFactory` instance.

Closes gh-38205
This commit is contained in:
Phillip Webb 2023-11-07 11:53:10 -08:00
parent f22c1ba7d6
commit 690cfa220a
2 changed files with 48 additions and 12 deletions

View File

@ -196,8 +196,7 @@ public class SpringBootContextLoader extends AbstractContextLoader implements Ao
else { else {
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
} }
application.setApplicationContextFactory( application.setApplicationContextFactory(getApplicationContextFactory(mergedConfig));
(webApplicationType) -> getApplicationContextFactory(mergedConfig, webApplicationType));
if (mergedConfig.getParent() != null) { if (mergedConfig.getParent() != null) {
application.setBannerMode(Banner.Mode.OFF); application.setBannerMode(Banner.Mode.OFF);
} }
@ -212,17 +211,26 @@ public class SpringBootContextLoader extends AbstractContextLoader implements Ao
} }
} }
private ConfigurableApplicationContext getApplicationContextFactory(MergedContextConfiguration mergedConfig, /**
WebApplicationType webApplicationType) { * Return the {@link ApplicationContextFactory} that should be used for the test. By
if (webApplicationType != WebApplicationType.NONE && !isEmbeddedWebEnvironment(mergedConfig)) { * default this method will return a factory that will create an appropriate
if (webApplicationType == WebApplicationType.REACTIVE) { * {@link ApplicationContext} for the {@link WebApplicationType}.
return new GenericReactiveWebApplicationContext(); * @param mergedConfig the merged context configuration
* @return the application context factory to use
* @since 3.2.0
*/
protected ApplicationContextFactory getApplicationContextFactory(MergedContextConfiguration mergedConfig) {
return (webApplicationType) -> {
if (webApplicationType != WebApplicationType.NONE && !isEmbeddedWebEnvironment(mergedConfig)) {
if (webApplicationType == WebApplicationType.REACTIVE) {
return new GenericReactiveWebApplicationContext();
}
if (webApplicationType == WebApplicationType.SERVLET) {
return new GenericWebApplicationContext();
}
} }
if (webApplicationType == WebApplicationType.SERVLET) { return ApplicationContextFactory.DEFAULT.create(webApplicationType);
return new GenericWebApplicationContext(); };
}
}
return ApplicationContextFactory.DEFAULT.create(webApplicationType);
} }
private void prepareEnvironment(MergedContextConfiguration mergedConfig, SpringApplication application, private void prepareEnvironment(MergedContextConfiguration mergedConfig, SpringApplication application,

View File

@ -26,12 +26,14 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.ApplicationContextFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest.UseMainMethod; import org.springframework.boot.test.context.SpringBootTest.UseMainMethod;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -246,6 +248,13 @@ class SpringBootContextLoaderTests {
.withMessage("UseMainMethod.ALWAYS cannot be used with @ContextHierarchy tests"); .withMessage("UseMainMethod.ALWAYS cannot be used with @ContextHierarchy tests");
} }
@Test
void whenSubclassProvidesCustomApplicationContextFactory() {
TestContext testContext = new ExposedTestContextManager(CustomApplicationContextTest.class)
.getExposedTestContext();
assertThat(testContext.getApplicationContext()).isInstanceOf(CustomAnnotationConfigApplicationContext.class);
}
private String[] getActiveProfiles(Class<?> testClass) { private String[] getActiveProfiles(Class<?> testClass) {
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext(); TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
ApplicationContext applicationContext = testContext.getApplicationContext(); ApplicationContext applicationContext = testContext.getApplicationContext();
@ -370,6 +379,25 @@ class SpringBootContextLoaderTests {
} }
@SpringBootTest
@ContextConfiguration(classes = Config.class, loader = CustomApplicationContextSpringBootContextLoader.class)
static class CustomApplicationContextTest {
}
static class CustomApplicationContextSpringBootContextLoader extends SpringBootContextLoader {
@Override
protected ApplicationContextFactory getApplicationContextFactory(MergedContextConfiguration mergedConfig) {
return (webApplicationType) -> new CustomAnnotationConfigApplicationContext();
}
}
static class CustomAnnotationConfigApplicationContext extends AnnotationConfigApplicationContext {
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class Config { static class Config {