use of org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo in project spring-framework by spring-projects.
the class HandlerResultMatchers method methodCall.
/**
* Assert the controller method used to process the request.
* <p>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
* public class SimpleController {
*
* @RequestMapping("/")
* public ResponseEntity<Void> handle() {
* return ResponseEntity.ok().build();
* }
* }
* </pre>
* <p>A test that has statically imported {@link MvcUriComponentsBuilder#on}
* can be performed as follows:
* <pre class="code">
* mockMvc.perform(get("/"))
* .andExpect(handler().methodCall(on(SimpleController.class).handle()));
* </pre>
*
* @param obj either the value returned from a "mock" controller invocation
* or the "mock" controller itself after an invocation
*/
public ResultMatcher methodCall(final Object obj) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
if (!MethodInvocationInfo.class.isInstance(obj)) {
fail(String.format("The supplied object [%s] is not an instance of %s. " + "Ensure that you invoke the handler method via MvcUriComponentsBuilder.on().", obj, MethodInvocationInfo.class.getName()));
}
MethodInvocationInfo invocationInfo = (MethodInvocationInfo) obj;
Method expected = invocationInfo.getControllerMethod();
Method actual = getHandlerMethod(result).getMethod();
assertEquals("Handler method", expected, actual);
}
};
}
Aggregations