Support target type in MockMvcResultMatchers.jsonPath()

This commit introduces an overloaded jsonPath() method to specify a
target type to coerce into for MockMvcResultMatchers.

 - jsonPath(String, Matcher<T>, Class<T>)

Closes gh-23141
This commit is contained in:
RustyTheClone
2019-06-15 20:36:07 +02:00
committed by Sam Brannen
parent 89ebdc766c
commit 72adc3d37e
4 changed files with 41 additions and 2 deletions

View File

@@ -204,6 +204,20 @@ public abstract class MockMvcResultMatchers {
return new JsonPathResultMatchers(expression).value(matcher);
}
/**
* An overloaded variant of {@link #jsonPath(String, Matcher)} (Matcher)} that also accepts
* a target type for the resulting value that the matcher can work reliably against.
* <p> This can be useful for matching numbers reliably &mdash; for example,
* to coerce an integer into a double.</p>
*
* @param expression the JSON path expression
* @param targetClass the target class to coerce the matching type into.
* @param matcher a matcher for the value expected at the JSON path
*/
public static <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
}
/**
* Access to response body assertions using an XPath expression to
* inspect a specific subset of the body.