类 JsonPathExpectationsHelper
- java.lang.Object
- org.springframework.test.util.JsonPathExpectationsHelper
public class JsonPathExpectationsHelper extends Object
A helper class for applying assertions via JSON path expressions.Based on the JsonPath project: requiring version 0.9+, with 1.1+ strongly recommended.
- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Craig Andrews, Sam Brannen
构造器概要
构造器 构造器 说明 JsonPathExpectationsHelper(String expression, Object... args)
Construct a newJsonPathExpectationsHelper
.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
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<T> matcher)
Evaluate the JSON path expression against the suppliedcontent
and assert the resulting value with the givenMatcher
.<T> void
assertValue(String content, Matcher<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.Object
evaluateJsonPath(String content)
Evaluate the JSON path and return the resulting value.Object
evaluateJsonPath(String content, Class<?> targetType)
Variant ofevaluateJsonPath(String)
with a target type.void
exists(String content)
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.
构造器详细资料
JsonPathExpectationsHelper
public JsonPathExpectationsHelper(String expression, Object... args)
Construct a newJsonPathExpectationsHelper
.- 参数:
expression
- theJsonPath
expression; nevernull
or emptyargs
- arguments to parameterize theJsonPath
expression with, using formatting specifiers defined inString.format(String, Object...)
方法详细资料
assertValue
public <T> void assertValue(String content, Matcher<T> matcher)
Evaluate the JSON path expression against the suppliedcontent
and assert the resulting value with the givenMatcher
.- 参数:
content
- the JSON contentmatcher
- the matcher with which to assert the result
assertValue
public <T> void assertValue(String content, Matcher<T> matcher, Class<T> targetType)
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.- 参数:
content
- the JSON contentmatcher
- the matcher with which to assert the resulttargetType
- the expected type of the resulting value- 从以下版本开始:
- 4.3.3
assertValue
public void assertValue(String content, @Nullable Object expectedValue)
Evaluate the JSON path expression against the suppliedcontent
and assert that the result is equal to the expected value.- 参数:
content
- the JSON contentexpectedValue
- the expected value
assertValueIsString
public void assertValueIsString(String content)
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aString
.- 参数:
content
- the JSON content- 从以下版本开始:
- 4.2.1
assertValueIsBoolean
public void assertValueIsBoolean(String content)
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aBoolean
.- 参数:
content
- the JSON content- 从以下版本开始:
- 4.2.1
assertValueIsNumber
public void assertValueIsNumber(String content)
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aNumber
.- 参数:
content
- the JSON content- 从以下版本开始:
- 4.2.1
assertValueIsArray
public void assertValueIsArray(String content)
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is an array.- 参数:
content
- the JSON content
assertValueIsMap
public void assertValueIsMap(String content)
Evaluate the JSON path expression against the suppliedcontent
and assert that the resulting value is aMap
.- 参数:
content
- the JSON content- 从以下版本开始:
- 4.2.1
exists
public void exists(String content)
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.
- 参数:
content
- the JSON content
doesNotExist
public 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.Note that if the JSON path expression is not definite, this method asserts that the list of values at the given path is empty.
- 参数:
content
- the JSON content
assertValueIsEmpty
public void assertValueIsEmpty(String content)
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)
.- 参数:
content
- the JSON content
assertValueIsNotEmpty
public void assertValueIsNotEmpty(String content)
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)
.- 参数:
content
- the JSON content
hasJsonPath
public void hasJsonPath(String content)
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.
- 参数:
content
- the JSON content- 从以下版本开始:
- 5.0.3
doesNotHaveJsonPath
public 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.If the JSON path expression is not definite, this method asserts that the list of values at the given path is empty.
- 参数:
content
- the JSON content- 从以下版本开始:
- 5.0.3
evaluateJsonPath
@Nullable public Object evaluateJsonPath(String content)
Evaluate the JSON path and return the resulting value.- 参数:
content
- the content to evaluate against- 返回:
- the result of the evaluation
- 抛出:
AssertionError
- if the evaluation fails
evaluateJsonPath
public Object evaluateJsonPath(String content, Class<?> targetType)
Variant ofevaluateJsonPath(String)
with a target type.This can be useful for matching numbers reliably for example coercing an integer into a double.
- 参数:
content
- the content to evaluate against- 返回:
- the result of the evaluation
- 抛出:
AssertionError
- if the evaluation fails