Search in sources :

Example 1 with MethodInvocationInfo

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">
	 * &#064;RestController
	 * public class SimpleController {
	 *
	 *     &#064;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);
        }
    };
}
Also used : MethodInvocationInfo(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

Method (java.lang.reflect.Method)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1 MethodInvocationInfo (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo)1