Skip to content

Commit 8c127ad

Browse files
committed
Property init HandlerMapping in standalone MockMvc
Issue: SPR-13637
1 parent 62af99a commit 8c127ad

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ private void registerMvcSingletons(StubWebApplicationContext wac) {
342342
StaticRequestMappingHandlerMapping hm = config.getHandlerMapping();
343343
hm.setServletContext(wac.getServletContext());
344344
hm.setApplicationContext(wac);
345+
hm.afterPropertiesSet();
345346
hm.registerHandlers(this.controllers);
346347
wac.addBean("requestMappingHandlerMapping", hm);
347348

spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.http.converter.json.SpringHandlerInstantiator;
2929
import org.springframework.mock.web.test.MockHttpServletRequest;
3030
import org.springframework.stereotype.Controller;
31+
import org.springframework.web.bind.annotation.PathVariable;
3132
import org.springframework.web.bind.annotation.RequestMapping;
3233
import org.springframework.web.context.WebApplicationContext;
3334
import org.springframework.web.context.support.WebApplicationContextUtils;
@@ -50,9 +51,10 @@
5051
*/
5152
public class StandaloneMockMvcBuilderTests {
5253

53-
@Test // SPR-10825
54-
public void placeHoldersInRequestMapping() throws Exception {
54+
// SPR-10825
5555

56+
@Test
57+
public void placeHoldersInRequestMapping() throws Exception {
5658
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
5759
builder.addPlaceHolderValue("sys.login.ajax", "/foo");
5860
builder.build();
@@ -66,7 +68,29 @@ public void placeHoldersInRequestMapping() throws Exception {
6668
assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
6769
}
6870

69-
@Test // SPR-12553
71+
// SPR-13637
72+
73+
@Test
74+
public void suffixPatternMatch() throws Exception {
75+
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController());
76+
builder.setUseSuffixPatternMatch(false);
77+
builder.build();
78+
79+
RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class);
80+
81+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/persons");
82+
HandlerExecutionChain chain = hm.getHandler(request);
83+
assertNotNull(chain);
84+
assertEquals("persons", ((HandlerMethod) chain.getHandler()).getMethod().getName());
85+
86+
request = new MockHttpServletRequest("GET", "/persons.xml");
87+
chain = hm.getHandler(request);
88+
assertNull(chain);
89+
}
90+
91+
// SPR-12553
92+
93+
@Test
7094
public void applicationContextAttribute() {
7195
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
7296
builder.addPlaceHolderValue("sys.login.ajax", "/foo");
@@ -100,7 +124,9 @@ public void addFilterPatternContainsNull() {
100124
builder.addFilter(new ContinueFilter(), (String) null);
101125
}
102126

103-
@Test // SPR-13375
127+
// SPR-13375
128+
129+
@Test
104130
@SuppressWarnings("rawtypes")
105131
public void springHandlerInstantiator() {
106132
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController());
@@ -112,6 +138,7 @@ public void springHandlerInstantiator() {
112138

113139

114140
@Controller
141+
@SuppressWarnings("unused")
115142
private static class PlaceholderController {
116143

117144
@RequestMapping(value = "${sys.login.ajax}")
@@ -135,17 +162,26 @@ protected WebApplicationContext initWebAppContext() {
135162
}
136163

137164
@Controller
165+
@SuppressWarnings("unused")
138166
private static class PersonController {
167+
168+
@RequestMapping(value="/persons")
169+
public String persons() {
170+
return null;
171+
}
172+
139173
@RequestMapping(value="/forward")
140174
public String forward() {
141175
return "forward:/persons";
142176
}
143177
}
144178

145179
private class ContinueFilter extends OncePerRequestFilter {
180+
146181
@Override
147182
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
148183
FilterChain filterChain) throws ServletException, IOException {
184+
149185
filterChain.doFilter(request, response);
150186
}
151187
}

0 commit comments

Comments
 (0)