org.springframework.test.web.servlet.result

Class HandlerResultMatchers

  • java.lang.Object
    • org.springframework.test.web.servlet.result.HandlerResultMatchers


  • public class HandlerResultMatchers
    extends java.lang.Object
    Factory for assertions on the selected handler or handler method.

    An instance of this class is typically accessed via MockMvcResultMatchers.handler().

    Note: Expectations that assert the controller method used to process the request work only for requests processed with RequestMappingHandlerMapping and RequestMappingHandlerAdapter which is used by default with the Spring MVC Java config and XML namespace.

    Since:
    3.2
    Author:
    Rossen Stoyanchev, Sam Brannen
    • Constructor Summary

      Constructors 
      Modifier Constructor and Description
      protected HandlerResultMatchers()
      Protected constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      ResultMatcher handlerType(java.lang.Class<?> type)
      Assert the type of the handler that processed the request.
      ResultMatcher method(java.lang.reflect.Method method)
      Assert the controller method used to process the request.
      ResultMatcher methodCall(java.lang.Object obj)
      Assert the controller method used to process the request.
      ResultMatcher methodName(org.hamcrest.Matcher<? super java.lang.String> matcher)
      Assert the name of the controller method used to process the request using the given Hamcrest Matcher.
      ResultMatcher methodName(java.lang.String name)
      Assert the name of the controller method used to process the request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • handlerType

        public ResultMatcher handlerType(java.lang.Class<?> type)
        Assert the type of the handler that processed the request.
      • methodCall

        public ResultMatcher methodCall(java.lang.Object obj)
        Assert the controller method used to process the request.

        The expected method is specified through a "mock" controller method invocation similar to MvcUriComponentsBuilder.fromMethodCall(Object).

        For example, given this controller:

         @RestController
         public class SimpleController {
        
             @RequestMapping("/")
             public ResponseEntity<Void> handle() {
                 return ResponseEntity.ok().build();
             }
         }
         

        A test that has statically imported MvcUriComponentsBuilder.on(java.lang.Class<T>) can be performed as follows:

         mockMvc.perform(get("/"))
             .andExpect(handler().methodCall(on(SimpleController.class).handle()));
         
        Parameters:
        obj - either the value returned from a "mock" controller invocation or the "mock" controller itself after an invocation
      • methodName

        public ResultMatcher methodName(org.hamcrest.Matcher<? super java.lang.String> matcher)
        Assert the name of the controller method used to process the request using the given Hamcrest Matcher.
      • methodName

        public ResultMatcher methodName(java.lang.String name)
        Assert the name of the controller method used to process the request.
      • method

        public ResultMatcher method(java.lang.reflect.Method method)
        Assert the controller method used to process the request.