Skip to content

Commit 21ccd58

Browse files
committed
Merge branch '2.7.x'
2 parents b0fb212 + 4545d39 commit 21ccd58

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.util.Assert;
5656
import org.springframework.util.ObjectUtils;
5757
import org.springframework.util.StringUtils;
58+
import org.springframework.web.context.ConfigurableWebApplicationContext;
5859
import org.springframework.web.context.support.GenericWebApplicationContext;
5960

6061
/**
@@ -103,14 +104,21 @@ public ApplicationContext loadContext(MergedContextConfiguration config) throws
103104
}
104105
else if (config instanceof ReactiveWebMergedContextConfiguration) {
105106
application.setWebApplicationType(WebApplicationType.REACTIVE);
106-
if (!isEmbeddedWebEnvironment(config)) {
107-
application.setApplicationContextFactory(
108-
ApplicationContextFactory.of(GenericReactiveWebApplicationContext::new));
109-
}
110107
}
111108
else {
112109
application.setWebApplicationType(WebApplicationType.NONE);
113110
}
111+
application.setApplicationContextFactory((type) -> {
112+
if (type != WebApplicationType.NONE && !isEmbeddedWebEnvironment(config)) {
113+
if (type == WebApplicationType.REACTIVE) {
114+
return new GenericReactiveWebApplicationContext();
115+
}
116+
else if (type == WebApplicationType.SERVLET) {
117+
return new GenericWebApplicationContext();
118+
}
119+
}
120+
return ApplicationContextFactory.DEFAULT.create(type);
121+
});
114122
application.setInitializers(initializers);
115123
ConfigurableEnvironment environment = getEnvironment();
116124
if (environment != null) {
@@ -261,14 +269,38 @@ void configure(MergedContextConfiguration configuration, SpringApplication appli
261269
List<ApplicationContextInitializer<?>> initializers) {
262270
WebMergedContextConfiguration webConfiguration = (WebMergedContextConfiguration) configuration;
263271
addMockServletContext(initializers, webConfiguration);
264-
application.setApplicationContextFactory((webApplicationType) -> new GenericWebApplicationContext());
265272
}
266273

267274
private void addMockServletContext(List<ApplicationContextInitializer<?>> initializers,
268275
WebMergedContextConfiguration webConfiguration) {
269276
SpringBootMockServletContext servletContext = new SpringBootMockServletContext(
270277
webConfiguration.getResourceBasePath());
271-
initializers.add(0, new ServletContextApplicationContextInitializer(servletContext, true));
278+
initializers.add(0, new DefensiveWebApplicationContextInitializer(
279+
new ServletContextApplicationContextInitializer(servletContext, true)));
280+
}
281+
282+
/**
283+
* Decorator for {@link ServletContextApplicationContextInitializer} that prevents
284+
* a failure when the context type is not as was predicted when the initializer
285+
* was registered. This can occur when spring.main.web-application-type is set to
286+
* something other than servlet.
287+
*/
288+
private static final class DefensiveWebApplicationContextInitializer
289+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
290+
291+
private final ServletContextApplicationContextInitializer delegate;
292+
293+
private DefensiveWebApplicationContextInitializer(ServletContextApplicationContextInitializer delegate) {
294+
this.delegate = delegate;
295+
}
296+
297+
@Override
298+
public void initialize(ConfigurableApplicationContext applicationContext) {
299+
if (applicationContext instanceof ConfigurableWebApplicationContext) {
300+
this.delegate.initialize((ConfigurableWebApplicationContext) applicationContext);
301+
}
302+
}
303+
272304
}
273305

274306
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.boot.test.util.TestPropertyValues;
27+
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
2728
import org.springframework.context.ApplicationContext;
2829
import org.springframework.context.annotation.Configuration;
2930
import org.springframework.core.env.ConfigurableEnvironment;
@@ -36,6 +37,7 @@
3637
import org.springframework.test.context.TestPropertySource;
3738
import org.springframework.test.context.support.TestPropertySourceUtils;
3839
import org.springframework.test.util.ReflectionTestUtils;
40+
import org.springframework.web.context.WebApplicationContext;
3941

4042
import static org.assertj.core.api.Assertions.assertThat;
4143

@@ -146,6 +148,20 @@ void propertySourceOrdering() throws Exception {
146148
assertThat(last).startsWith("Config resource");
147149
}
148150

151+
@Test
152+
void whenEnvironmentChangesWebApplicationTypeToNoneThenContextTypeChangesAccordingly() {
153+
TestContext context = new ExposedTestContextManager(ChangingWebApplicationTypeToNone.class)
154+
.getExposedTestContext();
155+
assertThat(context.getApplicationContext()).isNotInstanceOf(WebApplicationContext.class);
156+
}
157+
158+
@Test
159+
void whenEnvironmentChangesWebApplicationTypeToReactiveThenContextTypeChangesAccordingly() {
160+
TestContext context = new ExposedTestContextManager(ChangingWebApplicationTypeToReactive.class)
161+
.getExposedTestContext();
162+
assertThat(context.getApplicationContext()).isInstanceOf(GenericReactiveWebApplicationContext.class);
163+
}
164+
149165
private String[] getActiveProfiles(Class<?> testClass) {
150166
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
151167
ApplicationContext applicationContext = testContext.getApplicationContext();
@@ -228,6 +244,16 @@ static class Config {
228244

229245
}
230246

247+
@SpringBootTest(classes = Config.class, args = "--spring.main.web-application-type=none")
248+
static class ChangingWebApplicationTypeToNone {
249+
250+
}
251+
252+
@SpringBootTest(classes = Config.class, args = "--spring.main.web-application-type=reactive")
253+
static class ChangingWebApplicationTypeToReactive {
254+
255+
}
256+
231257
/**
232258
* {@link TestContextManager} which exposes the {@link TestContext}.
233259
*/

0 commit comments

Comments
 (0)