Skip to content

Commit 645514f

Browse files
committed
Polish "Honour base path from @WebAppConfiguration in @WebMvcTest"
See gh-16485
1 parent 7340d54 commit 645514f

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class WebMvcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
3434
@Override
3535
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
3636
MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig);
37-
return new WebMergedContextConfiguration(processedMergedConfiguration,
38-
getServletResourceBasePath(mergedConfig));
37+
return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig));
3938
}
4039

4140
@Override

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWithWebAppConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import static org.assertj.core.api.Assertions.assertThat;
3333

3434
/**
35-
* Tests for {@link WebMvcTest} when loading resources via {@link ServletContext} with
36-
* {@link WebAppConfiguration}.
35+
* Tests for {@link WebMvcTest @WebMvcTest} when loading resources via the
36+
* {@link ServletContext} with {@link WebAppConfiguration @WebAppConfiguration}.
3737
*
3838
* @author Lorenzo Dee
3939
*/
@@ -46,7 +46,7 @@ public class WebMvcTestWithWebAppConfigurationTests {
4646
private ServletContext servletContext;
4747

4848
@Test
49-
public void getResourceLocation() throws Exception {
49+
public void whenBasePathIsCustomizedResourcesCanBeLoadedFromThatLocation() throws Exception {
5050
testResource("/inwebapp", "src/test/webapp");
5151
testResource("/inmetainfresources", "/META-INF/resources");
5252
testResource("/inresources", "/resources");

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ protected MergedContextConfiguration processMergedContextConfiguration(MergedCon
154154
WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);
155155
if (webApplicationType == WebApplicationType.SERVLET
156156
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
157-
mergedConfig = new WebMergedContextConfiguration(mergedConfig,
158-
getServletResourceBasePath(mergedConfig));
157+
mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
159158
}
160159
else if (webApplicationType == WebApplicationType.REACTIVE
161160
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
@@ -187,7 +186,15 @@ private WebApplicationType deduceWebApplicationType() {
187186
return WebApplicationType.SERVLET;
188187
}
189188

190-
protected String getServletResourceBasePath(MergedContextConfiguration configuration) {
189+
/**
190+
* Determines the resource base path for web applications using the value of
191+
* {@link WebAppConfiguration @WebAppConfiguration}, if any, on the test class of the
192+
* given {@code configuration}. Defaults to {@code src/main/webapp} in its absence.
193+
* @param configuration the configure to examine
194+
* @return the resource base path
195+
* @since 2.1.6
196+
*/
197+
protected String determineResourceBasePath(MergedContextConfiguration configuration) {
191198
WebAppConfiguration webAppConfiguration = AnnotatedElementUtils
192199
.findMergedAnnotation(configuration.getTestClass(), WebAppConfiguration.class);
193200
return (webAppConfiguration != null) ? webAppConfiguration.value() : "src/main/webapp";

0 commit comments

Comments
 (0)