Skip to content

Commit 709e19f

Browse files
committed
Test ApplicationServetEnvironment is configured in web environments
See gh-48254
1 parent b72628c commit 709e19f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.context.annotation.Configuration;
5151
import org.springframework.context.support.AbstractApplicationContext;
5252
import org.springframework.core.Ordered;
53+
import org.springframework.core.env.ConfigurableEnvironment;
5354
import org.springframework.core.env.PropertySource;
5455
import org.springframework.mock.web.MockServletContext;
5556
import org.springframework.test.util.ReflectionTestUtils;
@@ -190,6 +191,29 @@ void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured
190191
}
191192
}
192193

194+
@Test
195+
void environmentIsConfiguredWithStandardServletEnvironment() {
196+
ServletContext servletContext = mock(ServletContext.class);
197+
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
198+
given(servletContext.getInitParameterNames())
199+
.willReturn(Collections.enumeration(Collections.singletonList("servlet.init.test")));
200+
given(servletContext.getInitParameter("servlet.init.test")).willReturn("from-servlet-context");
201+
given(servletContext.getAttributeNames())
202+
.willReturn(Collections.enumeration(Collections.singletonList("servlet.attribute.test")));
203+
given(servletContext.getAttribute("servlet.attribute.test")).willReturn("also-from-servlet-context");
204+
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new RegularSpringBootServletInitializer()
205+
.createRootApplicationContext(servletContext)) {
206+
assertThat(context).isNotNull();
207+
ConfigurableEnvironment environment = context.getEnvironment();
208+
assertThat(environment).isInstanceOf(StandardServletEnvironment.class);
209+
assertThat(environment.getClass().getName()).endsWith("ApplicationServletEnvironment");
210+
assertThat(environment.getPropertySources()).map(PropertySource::getName)
211+
.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
212+
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
213+
assertThat(environment.getProperty("servlet.init.test")).isEqualTo("from-servlet-context");
214+
}
215+
}
216+
193217
@Test
194218
void servletContextPropertySourceIsAvailablePriorToRefresh() {
195219
ServletContext servletContext = mock(ServletContext.class);
@@ -351,6 +375,15 @@ public SpringApplication build() {
351375

352376
}
353377

378+
static class RegularSpringBootServletInitializer extends SpringBootServletInitializer {
379+
380+
@Override
381+
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
382+
return builder.sources(TestApp.class);
383+
}
384+
385+
}
386+
354387
private static final class PropertySourceVerifyingApplicationListener
355388
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
356389

0 commit comments

Comments
 (0)