Honour base path from @WebAppConfiguration in @WebMvcTest

See gh-16485
This commit is contained in:
Lorenzo Dee
2019-04-08 17:20:06 +08:00
committed by Andy Wilkinson
parent 6f4d598418
commit 7340d546b8
10 changed files with 136 additions and 6 deletions

View File

@@ -71,6 +71,7 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson
* @author Brian Clozel
* @author Madhura Bhave
* @author Lorenzo Dee
* @since 1.4.0
* @see SpringBootTest
* @see TestConfiguration
@@ -153,11 +154,8 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);
if (webApplicationType == WebApplicationType.SERVLET
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
WebAppConfiguration webAppConfiguration = AnnotatedElementUtils
.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class);
String resourceBasePath = (webAppConfiguration != null) ? webAppConfiguration.value()
: "src/main/webapp";
mergedConfig = new WebMergedContextConfiguration(mergedConfig, resourceBasePath);
mergedConfig = new WebMergedContextConfiguration(mergedConfig,
getServletResourceBasePath(mergedConfig));
}
else if (webApplicationType == WebApplicationType.REACTIVE
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
@@ -189,6 +187,12 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
return WebApplicationType.SERVLET;
}
protected String getServletResourceBasePath(MergedContextConfiguration configuration) {
WebAppConfiguration webAppConfiguration = AnnotatedElementUtils
.findMergedAnnotation(configuration.getTestClass(), WebAppConfiguration.class);
return (webAppConfiguration != null) ? webAppConfiguration.value() : "src/main/webapp";
}
private boolean isWebEnvironmentSupported(MergedContextConfiguration mergedConfig) {
Class<?> testClass = mergedConfig.getTestClass();
ContextHierarchy hierarchy = AnnotationUtils.getAnnotation(testClass, ContextHierarchy.class);