From 645514f84821ba1a15033467c6ce474aa6690bf1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 17 Jun 2019 13:46:25 +0100 Subject: [PATCH] Polish "Honour base path from @WebAppConfiguration in @WebMvcTest" See gh-16485 --- .../web/servlet/WebMvcTestContextBootstrapper.java | 3 +-- .../WebMvcTestWithWebAppConfigurationTests.java | 6 +++--- .../context/SpringBootTestContextBootstrapper.java | 13 ++++++++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java index 2ce9490dc8..fab22fb500 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java @@ -34,8 +34,7 @@ class WebMvcTestContextBootstrapper extends SpringBootTestContextBootstrapper { @Override protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig); - return new WebMergedContextConfiguration(processedMergedConfiguration, - getServletResourceBasePath(mergedConfig)); + return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig)); } @Override diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWithWebAppConfigurationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWithWebAppConfigurationTests.java index 70e6c4eb4e..30705ce99f 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWithWebAppConfigurationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWithWebAppConfigurationTests.java @@ -32,8 +32,8 @@ import org.springframework.test.context.web.WebAppConfiguration; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link WebMvcTest} when loading resources via {@link ServletContext} with - * {@link WebAppConfiguration}. + * Tests for {@link WebMvcTest @WebMvcTest} when loading resources via the + * {@link ServletContext} with {@link WebAppConfiguration @WebAppConfiguration}. * * @author Lorenzo Dee */ @@ -46,7 +46,7 @@ public class WebMvcTestWithWebAppConfigurationTests { private ServletContext servletContext; @Test - public void getResourceLocation() throws Exception { + public void whenBasePathIsCustomizedResourcesCanBeLoadedFromThatLocation() throws Exception { testResource("/inwebapp", "src/test/webapp"); testResource("/inmetainfresources", "/META-INF/resources"); testResource("/inresources", "/resources"); diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java index e317f5e5c3..ce18816778 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java @@ -154,8 +154,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr WebApplicationType webApplicationType = getWebApplicationType(mergedConfig); if (webApplicationType == WebApplicationType.SERVLET && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) { - mergedConfig = new WebMergedContextConfiguration(mergedConfig, - getServletResourceBasePath(mergedConfig)); + mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig)); } else if (webApplicationType == WebApplicationType.REACTIVE && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) { @@ -187,7 +186,15 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr return WebApplicationType.SERVLET; } - protected String getServletResourceBasePath(MergedContextConfiguration configuration) { + /** + * Determines the resource base path for web applications using the value of + * {@link WebAppConfiguration @WebAppConfiguration}, if any, on the test class of the + * given {@code configuration}. Defaults to {@code src/main/webapp} in its absence. + * @param configuration the configure to examine + * @return the resource base path + * @since 2.1.6 + */ + protected String determineResourceBasePath(MergedContextConfiguration configuration) { WebAppConfiguration webAppConfiguration = AnnotatedElementUtils .findMergedAnnotation(configuration.getTestClass(), WebAppConfiguration.class); return (webAppConfiguration != null) ? webAppConfiguration.value() : "src/main/webapp";