Skip to content

Commit 318701d

Browse files
committed
Apply DispatcherServlet customizations to MockMvc
This commits makes sure that customizations on `DispatcherServlet` are also applied to the `TestDispatcherServlet` that `MockMvc` is using internally. Closes gh-5891
1 parent 18c2a2f commit 318701d

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,50 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2424
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.web.WebMvcProperties;
2526
import org.springframework.boot.context.properties.ConfigurationProperties;
2627
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2728
import org.springframework.context.annotation.Bean;
2829
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
2931
import org.springframework.test.web.servlet.MockMvc;
3032
import org.springframework.test.web.servlet.MockMvcBuilder;
3133
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
3234
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3335
import org.springframework.web.context.WebApplicationContext;
36+
import org.springframework.web.servlet.DispatcherServlet;
3437

3538
/**
3639
* Auto-configuration for {@link MockMvc}.
3740
*
3841
* @author Phillip Webb
3942
* @author Andy Wilkinson
43+
* @author Stephane Nicoll
4044
* @see AutoConfigureWebMvc
4145
* @since 1.4.0
4246
*/
4347
@Configuration
4448
@ConditionalOnWebApplication
4549
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
46-
@EnableConfigurationProperties
50+
@EnableConfigurationProperties(WebMvcProperties.class)
4751
public class MockMvcAutoConfiguration {
4852

4953
private final WebApplicationContext context;
54+
private final WebMvcProperties webMvcProperties;
5055

51-
MockMvcAutoConfiguration(WebApplicationContext context) {
56+
MockMvcAutoConfiguration(WebApplicationContext context,
57+
WebMvcProperties webMvcProperties) {
5258
this.context = context;
59+
this.webMvcProperties = webMvcProperties;
5360
}
5461

5562
@Bean
5663
@ConditionalOnMissingBean(MockMvcBuilder.class)
5764
public DefaultMockMvcBuilder mockMvcBuilder(
5865
List<MockMvcBuilderCustomizer> customizers) {
5966
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.context);
67+
builder.addDispatcherServletCustomizer(
68+
new MockMvcDispatcherServletCustomizer(this.webMvcProperties));
6069
for (MockMvcBuilderCustomizer customizer : customizers) {
6170
customizer.customize(builder);
6271
}
@@ -75,4 +84,25 @@ public MockMvc mockMvc(MockMvcBuilder builder) {
7584
return builder.build();
7685
}
7786

87+
private static class MockMvcDispatcherServletCustomizer
88+
implements DispatcherServletCustomizer {
89+
90+
private final WebMvcProperties webMvcProperties;
91+
92+
MockMvcDispatcherServletCustomizer(WebMvcProperties webMvcProperties) {
93+
this.webMvcProperties = webMvcProperties;
94+
}
95+
96+
@Override
97+
public void customize(DispatcherServlet dispatcherServlet) {
98+
dispatcherServlet.setDispatchOptionsRequest(
99+
this.webMvcProperties.isDispatchOptionsRequest());
100+
dispatcherServlet.setDispatchTraceRequest(
101+
this.webMvcProperties.isDispatchTraceRequest());
102+
dispatcherServlet.setThrowExceptionIfNoHandlerFound(
103+
this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
104+
}
105+
106+
}
107+
78108
}

spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/ExampleControllerAdvice.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package org.springframework.boot.test.autoconfigure.web.servlet;
1818

19+
import org.springframework.http.HttpStatus;
1920
import org.springframework.http.ResponseEntity;
2021
import org.springframework.web.bind.annotation.ControllerAdvice;
2122
import org.springframework.web.bind.annotation.ExceptionHandler;
23+
import org.springframework.web.bind.annotation.ResponseStatus;
24+
import org.springframework.web.servlet.NoHandlerFoundException;
2225

2326
/**
2427
* Example {@link ControllerAdvice} used with {@link WebMvcTest} tests.
2528
*
2629
* @author Phillip Webb
30+
* @author Stephane Nicoll
2731
*/
2832
@ControllerAdvice
2933
public class ExampleControllerAdvice {
@@ -33,4 +37,11 @@ public ResponseEntity<String> onExampleError(ExampleException exception) {
3337
return ResponseEntity.ok("recovered");
3438
}
3539

40+
@ExceptionHandler(NoHandlerFoundException.class)
41+
@ResponseStatus(HttpStatus.BAD_REQUEST)
42+
public ResponseEntity<String> noHandlerFoundHandler(NoHandlerFoundException exception) {
43+
return ResponseEntity.badRequest()
44+
.body("Invalid request: " + exception.getRequestURL());
45+
}
46+
3647
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.test.context.TestPropertySource;
24+
import org.springframework.test.context.junit4.SpringRunner;
25+
import org.springframework.test.web.servlet.MockMvc;
26+
import org.springframework.web.servlet.DispatcherServlet;
27+
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
/**
33+
* Tests for Test {@link DispatcherServlet} customizations.
34+
*
35+
* @author Stephane Nicoll
36+
*/
37+
@RunWith(SpringRunner.class)
38+
@WebMvcTest(secure = false)
39+
@TestPropertySource(properties = { "spring.mvc.throw-exception-if-no-handler-found=true",
40+
"spring.mvc.static-path-pattern=/static/**" })
41+
public class WebMvcTestCustomDispatcherServletIntegrationTests {
42+
43+
@Autowired
44+
private MockMvc mvc;
45+
46+
@Test
47+
public void dispatcherServletIsCustomized() throws Exception {
48+
this.mvc.perform(get("/does-not-exist"))
49+
.andExpect(status().isBadRequest())
50+
.andExpect(content().string("Invalid request: /does-not-exist"));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)