Skip to content

Commit 4b11835

Browse files
committed
Add methodCall option to HandlerResultMatchers
Issue: SPR-13736
1 parent 1320da9 commit 4b11835

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.test.web.servlet.ResultMatcher;
2525
import org.springframework.util.ClassUtils;
2626
import org.springframework.web.method.HandlerMethod;
27+
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
2728
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
2829
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
2930

@@ -73,6 +74,38 @@ public void match(MvcResult result) throws Exception {
7374
};
7475
}
7576

77+
/**
78+
* Assert the controller method used to process the request. The expected
79+
* method is specified through a "mock" controller method invocation
80+
* similar to {@link MvcUriComponentsBuilder#fromMethodCall(Object)}.
81+
* <p>For example given this controller:
82+
* <pre class="code">
83+
* &#064;RestController
84+
* static class SimpleController {
85+
*
86+
* &#064;RequestMapping("/")
87+
* public ResponseEntity<Void> handle() {
88+
* return ResponseEntity.ok().build();
89+
* }
90+
* }
91+
* </pre>
92+
* <p>A test can be performed:
93+
* <pre class="code">
94+
* mockMvc.perform(get("/"))
95+
* .andExpect(handler().methodCall(on(SimpleController.class).handle()));
96+
* </pre>
97+
*/
98+
public ResultMatcher methodCall(final Object info) {
99+
return new ResultMatcher() {
100+
@Override
101+
public void match(MvcResult result) throws Exception {
102+
HandlerMethod handlerMethod = getHandlerMethod(result);
103+
Method method = ((MvcUriComponentsBuilder.MethodInvocationInfo) info).getControllerMethod();
104+
assertEquals("HandlerMethod", method, handlerMethod.getMethod());
105+
}
106+
};
107+
}
108+
76109
/**
77110
* Assert the name of the controller method used to process the request
78111
* using the given Hamcrest {@link Matcher}.

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
import org.junit.Before;
2222
import org.junit.Test;
2323

24-
import org.springframework.http.HttpEntity;
2524
import org.springframework.http.ResponseEntity;
2625
import org.springframework.stereotype.Controller;
2726
import org.springframework.test.web.servlet.MockMvc;
2827
import org.springframework.web.bind.annotation.RequestMapping;
2928
import org.springframework.web.bind.annotation.ResponseBody;
3029

31-
import static org.hamcrest.Matchers.*;
32-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
33-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
34-
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
30+
import static org.hamcrest.Matchers.equalTo;
31+
import static org.hamcrest.Matchers.is;
32+
import static org.hamcrest.Matchers.not;
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
3537
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on;
3638

3739
/**
@@ -53,6 +55,11 @@ public void testHandlerType() throws Exception {
5355
this.mockMvc.perform(get("/")).andExpect(handler().handlerType(SimpleController.class));
5456
}
5557

58+
@Test
59+
public void testMethodCall() throws Exception {
60+
this.mockMvc.perform(get("/")).andExpect(handler().methodCall(on(SimpleController.class).handle()));
61+
}
62+
5663
@Test
5764
public void testHandlerMethodNameEqualTo() throws Exception {
5865
this.mockMvc.perform(get("/")).andExpect(handler().methodName("handle"));

0 commit comments

Comments
 (0)