Class MockMvcResultMatchers
- java.lang.Object
- org.springframework.test.web.servlet.result.MockMvcResultMatchers
public abstract class MockMvcResultMatchers extends Object
Static factory methods forResultMatcher
-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 Summary
Constructors Constructor Description MockMvcResultMatchers()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ContentResultMatchers
content()
Access to response body assertions.static CookieResultMatchers
cookie()
Access to response cookie assertions.static FlashAttributeResultMatchers
flash()
Access to flash attribute assertions.static ResultMatcher
forwardedUrl(String expectedUrl)
Asserts the request was forwarded to the given URL.static ResultMatcher
forwardedUrlPattern(String urlPattern)
Asserts the request was forwarded to the given URL.static ResultMatcher
forwardedUrlTemplate(String urlTemplate, Object... uriVars)
Asserts the request was forwarded to the given URL template.static HandlerResultMatchers
handler()
Access to assertions for the handler that handled the request.static HeaderResultMatchers
header()
Access to response header assertions.static JsonPathResultMatchers
jsonPath(String expression, Object... args)
Access to response body assertions using a JsonPath expression to inspect a specific subset of the body.static <T> ResultMatcher
jsonPath(String expression, Matcher<T> matcher)
static <T> ResultMatcher
jsonPath(String expression, Matcher<T> matcher, Class<T> targetType)
static ModelResultMatchers
model()
Access to model-related assertions.static ResultMatcher
redirectedUrl(String expectedUrl)
Asserts the request was redirected to the given URL.static ResultMatcher
redirectedUrlPattern(String urlPattern)
Asserts the request was redirected to the given URL.static ResultMatcher
redirectedUrlTemplate(String urlTemplate, Object... uriVars)
Asserts the request was redirected to the given URL template.static RequestResultMatchers
request()
Access to request-related assertions.static StatusResultMatchers
status()
Access to response status assertions.static ViewResultMatchers
view()
Access to assertions on the selected view.static XpathResultMatchers
xpath(String expression, Object... args)
Access to response body assertions using an XPath expression to inspect a specific subset of the body.static XpathResultMatchers
xpath(String expression, Map<String,String> namespaces, Object... args)
Access to response body assertions using an XPath expression to inspect a specific subset of the body.
Constructor Detail
MockMvcResultMatchers
public MockMvcResultMatchers()
Method Detail
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 encodeduriVars
- zero or more URI variables to populate the template- See Also:
UriComponentsBuilder.fromUriString(String)
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:
AntPathMatcher
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 encodeduriVars
- zero or more URI variables to populate the template- See Also:
UriComponentsBuilder.fromUriString(String)
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:
AntPathMatcher
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 argumentsargs
- arguments to parameterize the JSON path expression with- See Also:
jsonPath(String, Matcher)
,jsonPath(String, Matcher, Class)
jsonPath
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher)
Evaluate the given JsonPath expression against the response body and assert the resulting value with the given HamcrestMatcher
.- Parameters:
expression
- the JSON path expressionmatcher
- a matcher for the value expected at the JSON path- See Also:
jsonPath(String, Object...)
,jsonPath(String, Matcher, Class)
jsonPath
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType)
Evaluate the given JsonPath expression against the response body and assert the resulting value with the given HamcrestMatcher
, 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 expressionmatcher
- a matcher for the value expected at the JSON pathtargetType
- the target type to coerce the matching value into- Since:
- 5.2
- See Also:
jsonPath(String, Object...)
,jsonPath(String, Matcher)
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 argumentsargs
- 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 argumentsnamespaces
- the namespaces referenced in the XPath expressionargs
- arguments to parameterize the XPath expression with- Throws:
XPathExpressionException
cookie
public static CookieResultMatchers cookie()
Access to response cookie assertions.