Skip to content

Commit ceace66

Browse files
committed
Merge pull request #16485 from lorenzodee
* gh-16485: Polish "Honour base path from @WebAppConfiguration in @WebMvcTest" Honour base path from @WebAppConfiguration in @WebMvcTest Closes gh-16485
2 parents 6f4d598 + 645514f commit ceace66

File tree

10 files changed

+142
-6
lines changed

10 files changed

+142
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
*
2828
* @author Phillip Webb
2929
* @author Artsiom Yudovin
30+
* @author Lorenzo Dee
3031
*/
3132
class WebMvcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
3233

3334
@Override
3435
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
35-
return new WebMergedContextConfiguration(super.processMergedContextConfiguration(mergedConfig), "");
36+
MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig);
37+
return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig));
3638
}
3739

3840
@Override

spring-boot-project/spring-boot-test-autoconfigure/src/main/webapp/inwebapp

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
22+
import javax.servlet.ServletContext;
23+
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link WebMvcTest} when loading resources via {@link ServletContext}.
35+
*
36+
* @author Lorenzo Dee
37+
*/
38+
@RunWith(SpringRunner.class)
39+
@WebMvcTest
40+
public class WebMvcTestServletContextResourceTests {
41+
42+
@Autowired
43+
private ServletContext servletContext;
44+
45+
@Test
46+
public void getResourceLocation() throws Exception {
47+
testResource("/inwebapp", "src/main/webapp");
48+
testResource("/inmetainfresources", "/META-INF/resources");
49+
testResource("/inresources", "/resources");
50+
testResource("/instatic", "/static");
51+
testResource("/inpublic", "/public");
52+
}
53+
54+
private void testResource(String path, String expectedLocation) throws MalformedURLException {
55+
URL resource = this.servletContext.getResource(path);
56+
assertThat(resource).isNotNull();
57+
assertThat(resource.getPath()).contains(expectedLocation);
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
22+
import javax.servlet.ServletContext;
23+
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
import org.springframework.test.context.web.WebAppConfiguration;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests for {@link WebMvcTest @WebMvcTest} when loading resources via the
36+
* {@link ServletContext} with {@link WebAppConfiguration @WebAppConfiguration}.
37+
*
38+
* @author Lorenzo Dee
39+
*/
40+
@RunWith(SpringRunner.class)
41+
@WebMvcTest
42+
@WebAppConfiguration("src/test/webapp")
43+
public class WebMvcTestWithWebAppConfigurationTests {
44+
45+
@Autowired
46+
private ServletContext servletContext;
47+
48+
@Test
49+
public void whenBasePathIsCustomizedResourcesCanBeLoadedFromThatLocation() throws Exception {
50+
testResource("/inwebapp", "src/test/webapp");
51+
testResource("/inmetainfresources", "/META-INF/resources");
52+
testResource("/inresources", "/resources");
53+
testResource("/instatic", "/static");
54+
testResource("/inpublic", "/public");
55+
}
56+
57+
private void testResource(String path, String expectedLocation) throws MalformedURLException {
58+
URL resource = this.servletContext.getResource(path);
59+
assertThat(resource).isNotNull();
60+
assertThat(resource.getPath()).contains(expectedLocation);
61+
}
62+
63+
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/resources/META-INF/resources/inmetainfresources

Whitespace-only changes.

spring-boot-project/spring-boot-test-autoconfigure/src/test/resources/public/inpublic

Whitespace-only changes.

spring-boot-project/spring-boot-test-autoconfigure/src/test/resources/resources/inresources

Whitespace-only changes.

spring-boot-project/spring-boot-test-autoconfigure/src/test/resources/static/instatic

Whitespace-only changes.

spring-boot-project/spring-boot-test-autoconfigure/src/test/webapp/inwebapp

Whitespace-only changes.

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @author Andy Wilkinson
7272
* @author Brian Clozel
7373
* @author Madhura Bhave
74+
* @author Lorenzo Dee
7475
* @since 1.4.0
7576
* @see SpringBootTest
7677
* @see TestConfiguration
@@ -153,11 +154,7 @@ protected MergedContextConfiguration processMergedContextConfiguration(MergedCon
153154
WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);
154155
if (webApplicationType == WebApplicationType.SERVLET
155156
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
156-
WebAppConfiguration webAppConfiguration = AnnotatedElementUtils
157-
.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class);
158-
String resourceBasePath = (webAppConfiguration != null) ? webAppConfiguration.value()
159-
: "src/main/webapp";
160-
mergedConfig = new WebMergedContextConfiguration(mergedConfig, resourceBasePath);
157+
mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
161158
}
162159
else if (webApplicationType == WebApplicationType.REACTIVE
163160
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
@@ -189,6 +186,20 @@ private WebApplicationType deduceWebApplicationType() {
189186
return WebApplicationType.SERVLET;
190187
}
191188

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) {
198+
WebAppConfiguration webAppConfiguration = AnnotatedElementUtils
199+
.findMergedAnnotation(configuration.getTestClass(), WebAppConfiguration.class);
200+
return (webAppConfiguration != null) ? webAppConfiguration.value() : "src/main/webapp";
201+
}
202+
192203
private boolean isWebEnvironmentSupported(MergedContextConfiguration mergedConfig) {
193204
Class<?> testClass = mergedConfig.getTestClass();
194205
ContextHierarchy hierarchy = AnnotationUtils.getAnnotation(testClass, ContextHierarchy.class);

0 commit comments

Comments
 (0)