Skip to content

Commit a189e4c

Browse files
committed
Merge branch '2.1.x'
Closes gh-17220
2 parents 9d355f0 + ceace66 commit a189e4c

File tree

10 files changed

+137
-5
lines changed

10 files changed

+137
-5
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
@@ -28,12 +28,14 @@
2828
*
2929
* @author Phillip Webb
3030
* @author Artsiom Yudovin
31+
* @author Lorenzo Dee
3132
*/
3233
class WebMvcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
3334

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

3941
@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,58 @@
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.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link WebMvcTest @WebMvcTest} when loading resources via
33+
* {@link ServletContext}.
34+
*
35+
* @author Lorenzo Dee
36+
*/
37+
@WebMvcTest
38+
class WebMvcTestServletContextResourceTests {
39+
40+
@Autowired
41+
private ServletContext servletContext;
42+
43+
@Test
44+
void getResourceLocation() throws Exception {
45+
testResource("/inwebapp", "src/main/webapp");
46+
testResource("/inmetainfresources", "/META-INF/resources");
47+
testResource("/inresources", "/resources");
48+
testResource("/instatic", "/static");
49+
testResource("/inpublic", "/public");
50+
}
51+
52+
private void testResource(String path, String expectedLocation) throws MalformedURLException {
53+
URL resource = this.servletContext.getResource(path);
54+
assertThat(resource).isNotNull();
55+
assertThat(resource.getPath()).contains(expectedLocation);
56+
}
57+
58+
}
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.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
28+
import org.springframework.test.context.web.WebAppConfiguration;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link WebMvcTest @WebMvcTest} when loading resources via the
34+
* {@link ServletContext} with {@link WebAppConfiguration @WebAppConfiguration}.
35+
*
36+
* @author Lorenzo Dee
37+
*/
38+
@WebMvcTest
39+
@WebAppConfiguration("src/test/webapp")
40+
class WebMvcTestWithWebAppConfigurationTests {
41+
42+
@Autowired
43+
private ServletContext servletContext;
44+
45+
@Test
46+
void whenBasePathIsCustomizedResourcesCanBeLoadedFromThatLocation() throws Exception {
47+
testResource("/inwebapp", "src/test/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+
}

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Andy Wilkinson
7373
* @author Brian Clozel
7474
* @author Madhura Bhave
75+
* @author Lorenzo Dee
7576
* @since 1.4.0
7677
* @see SpringBootTest
7778
* @see TestConfiguration
@@ -154,10 +155,7 @@ protected MergedContextConfiguration processMergedContextConfiguration(MergedCon
154155
WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);
155156
if (webApplicationType == WebApplicationType.SERVLET
156157
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
157-
String resourceBasePath = MergedAnnotations.from(mergedConfig.getTestClass(), SearchStrategy.EXHAUSTIVE)
158-
.get(WebAppConfiguration.class).getValue(MergedAnnotation.VALUE, String.class)
159-
.orElse("src/main/webapp");
160-
mergedConfig = new WebMergedContextConfiguration(mergedConfig, resourceBasePath);
158+
mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
161159
}
162160
else if (webApplicationType == WebApplicationType.REACTIVE
163161
&& (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
@@ -189,6 +187,20 @@ private WebApplicationType deduceWebApplicationType() {
189187
return WebApplicationType.SERVLET;
190188
}
191189

190+
/**
191+
* Determines the resource base path for web applications using the value of
192+
* {@link WebAppConfiguration @WebAppConfiguration}, if any, on the test class of the
193+
* given {@code configuration}. Defaults to {@code src/main/webapp} in its absence.
194+
* @param configuration the configure to examine
195+
* @return the resource base path
196+
* @since 2.1.6
197+
*/
198+
protected String determineResourceBasePath(MergedContextConfiguration configuration) {
199+
return MergedAnnotations.from(configuration.getTestClass(), SearchStrategy.EXHAUSTIVE)
200+
.get(WebAppConfiguration.class).getValue(MergedAnnotation.VALUE, String.class)
201+
.orElse("src/main/webapp");
202+
}
203+
192204
private boolean isWebEnvironmentSupported(MergedContextConfiguration mergedConfig) {
193205
Class<?> testClass = mergedConfig.getTestClass();
194206
ContextHierarchy hierarchy = AnnotationUtils.getAnnotation(testClass, ContextHierarchy.class);

0 commit comments

Comments
 (0)