Class MockRestRequestMatchers

java.lang.Object
org.springframework.test.web.client.match.MockRestRequestMatchers

public abstract class MockRestRequestMatchers extends Object
Static factory methods for RequestMatcher classes. Typically used to provide input for MockRestServiceServer.expect(RequestMatcher).

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:
Craig Walls, Rossen Stoyanchev, Sam Brannen
  • Constructor Details

    • MockRestRequestMatchers

      public MockRestRequestMatchers()
  • Method Details

    • anything

      public static RequestMatcher anything()
      Match to any request.
    • method

      public static RequestMatcher method(HttpMethod method)
      Assert the HttpMethod of the request.
      Parameters:
      method - the HTTP method
      Returns:
      the request matcher
    • requestTo

      public static RequestMatcher requestTo(Matcher<? super String> matcher)
      Assert the request URI string with the given Hamcrest matcher.
      Parameters:
      matcher - the String matcher for the expected URI
      Returns:
      the request matcher
    • requestTo

      public static RequestMatcher requestTo(String expectedUri)
      Assert the request URI matches the given string.
      Parameters:
      expectedUri - the expected URI
      Returns:
      the request matcher
    • requestToUriTemplate

      public static RequestMatcher requestToUriTemplate(String expectedUri, Object... uriVars)
      Variant of requestTo(URI) that prepares the URI from a URI template plus optional variables via UriComponentsBuilder including encoding.
      Parameters:
      expectedUri - the expected URI template
      uriVars - zero or more URI variables to populate the expected URI
      Returns:
      the request matcher
    • requestTo

      public static RequestMatcher requestTo(URI uri)
      Expect a request to the given URI.
      Parameters:
      uri - the expected URI
      Returns:
      the request matcher
    • queryParam

      @SafeVarargs public static RequestMatcher queryParam(String name, Matcher<? super String>... matchers)
      Assert request query parameter values with the given Hamcrest matcher(s).

      Note that if the queryParam value list is larger than the number of provided matchers, extra values are considered acceptable. See queryParam(String, Matcher) for a variant that takes a Matcher over the whole list of values.

      See Also:
    • queryParam

      public static RequestMatcher queryParam(String name, String... expectedValues)
      Assert request query parameter values.

      Note that if the queryParam value list is larger than expectedValues, extra values are considered acceptable. See queryParam(String, Matcher) for a variant that takes a Matcher over the whole list of values.

      See Also:
    • queryParam

      public static RequestMatcher queryParam(String name, Matcher<? super List<String>> matcher)
      Assert request query parameter, matching on the whole List of values.

      This can be used to check that the list has at least one value matching a criteria (Matchers.hasItem(Matcher)), or that every value in the list matches a common criteria (Matchers.everyItem(Matcher)), or that each value in the list matches its corresponding dedicated criteria (Matchers.contains(Matcher[]), and more.

      Parameters:
      name - the name of the query parameter to consider
      matcher - the matcher to apply to the whole list of values for that header
      Since:
      6.0.5
    • header

      @SafeVarargs public static RequestMatcher header(String name, Matcher<? super String>... matchers)
      Assert request header values with the given Hamcrest matcher(s).

      Note that if the header's value list is larger than the number of provided matchers, extra values are considered acceptable. See header(String, Matcher) for a variant that takes a Matcher over the whole list of values.

      See Also:
    • header

      public static RequestMatcher header(String name, String... expectedValues)
      Assert request header values.

      Note that if the header's value list is larger than expectedValues, extra values are considered acceptable. See header(String, Matcher) for a variant that takes a Matcher over the whole list of values.

      See Also:
    • header

      public static RequestMatcher header(String name, Matcher<? super List<String>> matcher)
      Assert request header, matching on the whole List of values.

      This can be used to check that the list has at least one value matching a criteria (Matchers.hasItem(Matcher)), or that every value in the list matches a common criteria (Matchers.everyItem(Matcher)), or that each value in the list matches its corresponding dedicated criteria (Matchers.contains(Matcher[]), and more.

      Parameters:
      name - the name of the request header to consider
      matcher - the matcher to apply to the whole list of values for that header
      Since:
      6.0.5
    • headerDoesNotExist

      public static RequestMatcher headerDoesNotExist(String name)
      Assert that the given request header does not exist.
      Since:
      5.2
    • content

      public static ContentRequestMatchers content()
      Access to request body matchers.
    • jsonPath

      public static JsonPathRequestMatchers jsonPath(String expression, Object... args)
      Access to request body matchers 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 optionally parameterized with arguments
      args - arguments to parameterize the JSON path expression with
    • jsonPath

      public static <T> RequestMatcher jsonPath(String expression, Matcher<? super T> matcher)
      Access to request body matchers using a JsonPath expression to inspect a specific subset of the body and a Hamcrest match for asserting the value found at the JSON path.
      Parameters:
      expression - the JSON path expression
      matcher - a matcher for the value expected at the JSON path
    • xpath

      public static XpathRequestMatchers xpath(String expression, Object... args) throws XPathExpressionException
      Access to request body matchers using an XPath 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 optionally parameterized with arguments
      args - arguments to parameterize the XPath expression with
      Throws:
      XPathExpressionException
    • xpath

      public static XpathRequestMatchers xpath(String expression, Map<String,String> namespaces, Object... args) throws XPathExpressionException
      Access to response body matchers using an XPath 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 optionally parameterized with arguments
      namespaces - the namespaces referenced in the XPath expression
      args - arguments to parameterize the XPath expression with
      Throws:
      XPathExpressionException