Class JsonPathExpectationsHelper
Based on the JsonPath project: requiring version 0.9+, with 1.1+ strongly recommended.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Craig Andrews, Sam Brannen
-
Constructor Summary
ConstructorDescriptionJsonPathExpectationsHelper
(String expression, Object... args) Construct a newJsonPathExpectationsHelper
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
assertValue
(String content, Object expectedValue) Evaluate the JSON path expression against the suppliedcontent
and assert that the result is equal to the expected value.<T> void
assertValue
(String content, Matcher<? super T> matcher) Evaluate the JSON path expression against the suppliedcontent
and assert the resulting value with the givenMatcher
.<T> void
assertValue
(String content, Matcher<? super T> matcher, Class<T> targetType) An overloaded variant ofassertValue(String, Matcher)
that also accepts a target type for the resulting value.void
assertValueIsArray
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is an array.void
assertValueIsBoolean
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aBoolean
.void
assertValueIsEmpty
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that an empty value exists at the given path.void
assertValueIsMap
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aMap
.void
assertValueIsNotEmpty
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that a non-empty value exists at the given path.void
assertValueIsNumber
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aNumber
.void
assertValueIsString
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aString
.void
doesNotExist
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that a non-null value does not exist at the given path.void
doesNotHaveJsonPath
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that a value, includingnull
values, does not exist at the given path.evaluateJsonPath
(String content) Evaluate the JSON path and return the resulting value.evaluateJsonPath
(String content, Class<?> targetType) Variant ofevaluateJsonPath(String)
with a target type.void
Evaluate the JSON path expression against the suppliedcontent
and assert that a non-null value, possibly an empty array or map, exists at the given path.void
hasJsonPath
(String content) Evaluate the JSON path expression against the suppliedcontent
and assert that a value, possiblynull
, exists.
-
Constructor Details
-
JsonPathExpectationsHelper
Construct a newJsonPathExpectationsHelper
.- Parameters:
expression
- theJsonPath
expression; nevernull
or emptyargs
- arguments to parameterize theJsonPath
expression with, using formatting specifiers defined inString.format(String, Object...)
-
-
Method Details
-
assertValue
Evaluate the JSON path expression against the suppliedcontent
and assert the resulting value with the givenMatcher
.- Parameters:
content
- the JSON contentmatcher
- the matcher with which to assert the result
-
assertValue
An overloaded variant ofassertValue(String, Matcher)
that also accepts a target type for the resulting value. This can be useful for matching numbers reliably for example coercing an integer into a double.- Parameters:
content
- the JSON contentmatcher
- the matcher with which to assert the resulttargetType
- the expected type of the resulting value- Since:
- 4.3.3
-
assertValue
Evaluate the JSON path expression against the suppliedcontent
and assert that the result is equal to the expected value.- Parameters:
content
- the JSON contentexpectedValue
- the expected value
-
assertValueIsString
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aString
.- Parameters:
content
- the JSON content- Since:
- 4.2.1
-
assertValueIsBoolean
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aBoolean
.- Parameters:
content
- the JSON content- Since:
- 4.2.1
-
assertValueIsNumber
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aNumber
.- Parameters:
content
- the JSON content- Since:
- 4.2.1
-
assertValueIsArray
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is an array.- Parameters:
content
- the JSON content
-
assertValueIsMap
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aMap
.- Parameters:
content
- the JSON content- Since:
- 4.2.1
-
exists
Evaluate the JSON path expression against the suppliedcontent
and assert that a non-null value, possibly an empty array or map, exists at the given path.Note that if the JSON path expression is not definite, this method asserts that the list of values at the given path is not empty.
- Parameters:
content
- the JSON content
-
doesNotExist
Evaluate the JSON path expression against the suppliedcontent
and assert that a non-null value does not exist at the given path.Note that if the JSON path expression is not definite, this method asserts that the list of values at the given path is empty.
- Parameters:
content
- the JSON content
-
assertValueIsEmpty
Evaluate the JSON path expression against the suppliedcontent
and assert that an empty value exists at the given path.For the semantics of empty, consult the Javadoc for
ObjectUtils.isEmpty(Object)
.- Parameters:
content
- the JSON content
-
assertValueIsNotEmpty
Evaluate the JSON path expression against the suppliedcontent
and assert that a non-empty value exists at the given path.For the semantics of empty, consult the Javadoc for
ObjectUtils.isEmpty(Object)
.- Parameters:
content
- the JSON content
-
hasJsonPath
Evaluate the JSON path expression against the suppliedcontent
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.
- Parameters:
content
- the JSON content- Since:
- 5.0.3
-
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.
- Parameters:
content
- the JSON content- Since:
- 5.0.3
-
evaluateJsonPath
Evaluate the JSON path and return the resulting value.- Parameters:
content
- the content to evaluate against- Returns:
- the result of the evaluation
- Throws:
AssertionError
- if the evaluation fails
-
evaluateJsonPath
Variant ofevaluateJsonPath(String)
with a target type.This can be useful for matching numbers reliably for example coercing an integer into a double.
- Parameters:
content
- the content to evaluate against- Returns:
- the result of the evaluation
- Throws:
AssertionError
- if the evaluation fails
-