Class JsonPathRequestMatchers
- java.lang.Object
- org.springframework.test.web.client.match.JsonPathRequestMatchers
public class JsonPathRequestMatchers extends Object
Factory for assertions on the request content using JsonPath expressions.An instance of this class is typically accessed via
MockRestRequestMatchers.jsonPath(String, Matcher)
orMockRestRequestMatchers.jsonPath(String, Object...)
.- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Sam Brannen
Constructor Summary
Constructors Modifier Constructor Description protected
JsonPathRequestMatchers(String expression, Object... args)
Protected constructor.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestMatcher
doesNotExist()
Evaluate the JSON path expression against the request content and assert that a value does not exist at the given path.RequestMatcher
doesNotHaveJsonPath()
Evaluate the JSON path expression against the suppliedcontent
and assert that a value, includingnull
values, does not exist at the given path.RequestMatcher
exists()
Evaluate the JSON path expression against the request content and assert that a non-null value exists at the given path.RequestMatcher
hasJsonPath()
Evaluate the JSON path expression against the response content and assert that a value, possiblynull
, exists.RequestMatcher
isArray()
Evaluate the JSON path expression against the request content and assert that the result is an array.RequestMatcher
isBoolean()
Evaluate the JSON path expression against the request content and assert that the result is aBoolean
.RequestMatcher
isEmpty()
Evaluate the JSON path expression against the request content and assert that an empty value exists at the given path.RequestMatcher
isMap()
Evaluate the JSON path expression against the request content and assert that the result is aMap
.RequestMatcher
isNotEmpty()
Evaluate the JSON path expression against the request content and assert that a non-empty value exists at the given path.RequestMatcher
isNumber()
Evaluate the JSON path expression against the request content and assert that the result is aNumber
.RequestMatcher
isString()
Evaluate the JSON path expression against the request content and assert that the result is aString
.RequestMatcher
value(Object expectedValue)
Evaluate the JSON path expression against the request content and assert that the result is equal to the supplied value.<T> RequestMatcher
value(Matcher<T> matcher)
Evaluate the JSON path expression against the request content and assert the resulting value with the given HamcrestMatcher
.<T> RequestMatcher
value(Matcher<T> matcher, Class<T> targetType)
An overloaded variant ofvalue(Matcher)
that also accepts a target type for the resulting value that the matcher can work reliably against.
Constructor Detail
JsonPathRequestMatchers
protected JsonPathRequestMatchers(String expression, Object... args)
Protected constructor.Use
MockRestRequestMatchers.jsonPath(String, Matcher)
orMockRestRequestMatchers.jsonPath(String, Object...)
.- Parameters:
expression
- theJsonPath
expression; nevernull
or emptyargs
- arguments to parameterize theJsonPath
expression with, using formatting specifiers defined inString.format(String, Object...)
Method Detail
value
public <T> RequestMatcher value(Matcher<T> matcher)
Evaluate the JSON path expression against the request content and assert the resulting value with the given HamcrestMatcher
.
value
public <T> RequestMatcher value(Matcher<T> matcher, Class<T> targetType)
An overloaded variant ofvalue(Matcher)
that also accepts a target type for the resulting value that the matcher can work reliably against.This can be useful for matching numbers reliably — for example, to coerce an integer into a double.
- Since:
- 4.3.3
value
public RequestMatcher value(Object expectedValue)
Evaluate the JSON path expression against the request content and assert that the result is equal to the supplied value.
exists
public RequestMatcher exists()
Evaluate the JSON path expression against the request content and assert that a non-null value exists at the given path.If the JSON path expression is not definite, this method asserts that the value at the given path is not empty.
doesNotExist
public RequestMatcher doesNotExist()
Evaluate the JSON path expression against the request content and assert that a value does not exist at the given path.If the JSON path expression is not definite, this method asserts that the value at the given path is empty.
hasJsonPath
public RequestMatcher hasJsonPath()
Evaluate the JSON path expression against the response content and assert that a value, possiblynull
, exists.If the JSON path expression is not definite, this method asserts that the list of values at the given path is not empty.
- Since:
- 5.0.3
- See Also:
exists()
,isNotEmpty()
doesNotHaveJsonPath
public RequestMatcher doesNotHaveJsonPath()
Evaluate the JSON path expression against the suppliedcontent
and assert that a value, includingnull
values, does not exist at the given path.If the JSON path expression is not definite, this method asserts that the list of values at the given path is empty.
- Since:
- 5.0.3
- See Also:
doesNotExist()
,isEmpty()
isEmpty
public RequestMatcher isEmpty()
Evaluate the JSON path expression against the request content and assert that an empty value exists at the given path.For the semantics of empty, consult the Javadoc for
ObjectUtils.isEmpty(Object)
.- Since:
- 4.2.1
- See Also:
isNotEmpty()
,exists()
,doesNotExist()
isNotEmpty
public RequestMatcher isNotEmpty()
Evaluate the JSON path expression against the request content and assert that a non-empty value exists at the given path.For the semantics of empty, consult the Javadoc for
ObjectUtils.isEmpty(Object)
.- Since:
- 4.2.1
- See Also:
isEmpty()
,exists()
,doesNotExist()
isString
public RequestMatcher isString()
Evaluate the JSON path expression against the request content and assert that the result is aString
.- Since:
- 4.2.1
isBoolean
public RequestMatcher isBoolean()
Evaluate the JSON path expression against the request content and assert that the result is aBoolean
.- Since:
- 4.2.1
isNumber
public RequestMatcher isNumber()
Evaluate the JSON path expression against the request content and assert that the result is aNumber
.- Since:
- 4.2.1
isArray
public RequestMatcher isArray()
Evaluate the JSON path expression against the request content and assert that the result is an array.
isMap
public RequestMatcher isMap()
Evaluate the JSON path expression against the request content and assert that the result is aMap
.- Since:
- 4.2.1