Class MockMvcResultMatchers

java.lang.Object
org.springframework.test.web.servlet.result.MockMvcResultMatchers

public abstract class MockMvcResultMatchers extends Object
Static factory methods for ResultMatcher-based result actions.

Eclipse Users

Consider adding this class as a Java editor favorite. To navigate to this setting, open the Preferences and type "favorites".

Since:
3.2
Author:
Rossen Stoyanchev, Brian Clozel, Sam Brannen
  • Constructor Details

    • MockMvcResultMatchers

      public MockMvcResultMatchers()
  • Method Details

    • request

      public static RequestResultMatchers request()
      Access to request-related assertions.
    • handler

      public static HandlerResultMatchers handler()
      Access to assertions for the handler that handled the request.
    • model

      public static ModelResultMatchers model()
      Access to model-related assertions.
    • view

      public static ViewResultMatchers view()
      Access to assertions on the selected view.
    • flash

      public static FlashAttributeResultMatchers flash()
      Access to flash attribute assertions.
    • forwardedUrl

      public static ResultMatcher forwardedUrl(@Nullable String expectedUrl)
      Asserts the request was forwarded to the given URL.

      This method accepts only exact matches.

      Parameters:
      expectedUrl - the exact URL expected
    • forwardedUrlTemplate

      public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars)
      Asserts the request was forwarded to the given URL template.

      This method accepts exact matches against the expanded and encoded URL template.

      Parameters:
      urlTemplate - a URL template; the expanded URL will be encoded
      uriVars - zero or more URI variables to populate the template
      See Also:
    • forwardedUrlPattern

      public static ResultMatcher forwardedUrlPattern(String urlPattern)
      Asserts the request was forwarded to the given URL.

      This method accepts AntPathMatcher patterns.

      Parameters:
      urlPattern - an Ant-style path pattern to match against
      Since:
      4.0
      See Also:
    • redirectedUrl

      public static ResultMatcher redirectedUrl(String expectedUrl)
      Asserts the request was redirected to the given URL.

      This method accepts only exact matches.

      Parameters:
      expectedUrl - the exact URL expected
    • redirectedUrlTemplate

      public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars)
      Asserts the request was redirected to the given URL template.

      This method accepts exact matches against the expanded and encoded URL template.

      Parameters:
      urlTemplate - a URL template; the expanded URL will be encoded
      uriVars - zero or more URI variables to populate the template
      See Also:
    • redirectedUrlPattern

      public static ResultMatcher redirectedUrlPattern(String urlPattern)
      Asserts the request was redirected to the given URL.

      This method accepts AntPathMatcher patterns.

      Parameters:
      urlPattern - an Ant-style path pattern to match against
      Since:
      4.0
      See Also:
    • status

      public static StatusResultMatchers status()
      Access to response status assertions.
    • header

      public static HeaderResultMatchers header()
      Access to response header assertions.
    • content

      public static ContentResultMatchers content()
      Access to response body assertions.
    • jsonPath

      public static JsonPathResultMatchers jsonPath(String expression, Object... args)
      Access to response body assertions using a JsonPath expression to inspect a specific subset of the body.

      The JSON path expression can be a parameterized string using formatting specifiers as defined in String.format(String, Object...).

      Parameters:
      expression - the JSON path expression, optionally parameterized with arguments
      args - arguments to parameterize the JSON path expression with
      See Also:
    • jsonPath

      public static <T> ResultMatcher jsonPath(String expression, Matcher<? super T> matcher)
      Evaluate the given JsonPath expression against the response body and assert the resulting value with the given Hamcrest Matcher.
      Parameters:
      expression - the JSON path expression
      matcher - a matcher for the value expected at the JSON path
      See Also:
    • jsonPath

      public static <T> ResultMatcher jsonPath(String expression, Matcher<? super T> matcher, Class<T> targetType)
      Evaluate the given JsonPath expression against the response body and assert the resulting value with the given Hamcrest Matcher, coercing the resulting value into the given target type before applying the matcher.

      This can be useful for matching numbers reliably — for example, to coerce an integer into a double.

      Parameters:
      expression - the JSON path expression
      matcher - a matcher for the value expected at the JSON path
      targetType - the target type to coerce the matching value into
      Since:
      5.2
      See Also:
    • xpath

      public static XpathResultMatchers xpath(String expression, Object... args) throws XPathExpressionException
      Access to response body assertions using an XPath expression to inspect a specific subset of the body.

      The XPath expression can be a parameterized string using formatting specifiers as defined in String.format(String, Object...).

      Parameters:
      expression - the XPath expression, optionally parameterized with arguments
      args - arguments to parameterize the XPath expression with
      Throws:
      XPathExpressionException
    • xpath

      public static XpathResultMatchers xpath(String expression, Map<String,String> namespaces, Object... args) throws XPathExpressionException
      Access to response body assertions using an XPath expression to inspect a specific subset of the body.

      The XPath expression can be a parameterized string using formatting specifiers as defined in String.format(String, Object...).

      Parameters:
      expression - the XPath expression, optionally parameterized with arguments
      namespaces - the namespaces referenced in the XPath expression
      args - arguments to parameterize the XPath expression with
      Throws:
      XPathExpressionException
    • cookie

      public static CookieResultMatchers cookie()
      Access to response cookie assertions.