Add methodCall option to HandlerResultMatchers
Issue: SPR-13736
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultMatcher;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
@@ -73,6 +74,38 @@ public class HandlerResultMatchers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the controller method used to process the request. The expected
|
||||
* method is specified through a "mock" controller method invocation
|
||||
* similar to {@link MvcUriComponentsBuilder#fromMethodCall(Object)}.
|
||||
* <p>For example given this controller:
|
||||
* <pre class="code">
|
||||
* @RestController
|
||||
* static class SimpleController {
|
||||
*
|
||||
* @RequestMapping("/")
|
||||
* public ResponseEntity<Void> handle() {
|
||||
* return ResponseEntity.ok().build();
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* <p>A test can be performed:
|
||||
* <pre class="code">
|
||||
* mockMvc.perform(get("/"))
|
||||
* .andExpect(handler().methodCall(on(SimpleController.class).handle()));
|
||||
* </pre>
|
||||
*/
|
||||
public ResultMatcher methodCall(final Object info) {
|
||||
return new ResultMatcher() {
|
||||
@Override
|
||||
public void match(MvcResult result) throws Exception {
|
||||
HandlerMethod handlerMethod = getHandlerMethod(result);
|
||||
Method method = ((MvcUriComponentsBuilder.MethodInvocationInfo) info).getControllerMethod();
|
||||
assertEquals("HandlerMethod", method, handlerMethod.getMethod());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the name of the controller method used to process the request
|
||||
* using the given Hamcrest {@link Matcher}.
|
||||
|
||||
Reference in New Issue
Block a user