Package org.springframework.test.util
Class 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.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Craig Andrews, Sam Brannen
Constructor Summary
Constructors Constructor Description JsonPathExpectationsHelper(String expression, Object... args)Construct a newJsonPathExpectationsHelper.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidassertValue(String content, Object expectedValue)Evaluate the JSON path expression against the suppliedcontentand assert that the result is equal to the expected value.<T> voidassertValue(String content, Matcher<T> matcher)Evaluate the JSON path expression against the suppliedcontentand assert the resulting value with the givenMatcher.<T> voidassertValue(String content, Matcher<T> matcher, Class<T> targetType)An overloaded variant ofassertValue(String, Matcher)that also accepts a target type for the resulting value.voidassertValueIsArray(String content)Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is an array.voidassertValueIsBoolean(String content)Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aBoolean.voidassertValueIsEmpty(String content)Evaluate the JSON path expression against the suppliedcontentand assert that an empty value exists at the given path.voidassertValueIsMap(String content)Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aMap.voidassertValueIsNotEmpty(String content)Evaluate the JSON path expression against the suppliedcontentand assert that a non-empty value exists at the given path.voidassertValueIsNumber(String content)Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aNumber.voidassertValueIsString(String content)Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aString.voiddoesNotExist(String content)Evaluate the JSON path expression against the suppliedcontentand assert that a value does not exist at the given path.voidexists(String content)Evaluate the JSON path expression against the suppliedcontentand assert that a non-null value exists at the given path.
Constructor Detail
JsonPathExpectationsHelper
public JsonPathExpectationsHelper(String expression, Object... args)
Construct a newJsonPathExpectationsHelper.- Parameters:
expression- theJsonPathexpression; nevernullor emptyargs- arguments to parameterize theJsonPathexpression with, using formatting specifiers defined inString.format(String, Object...)
Method Detail
assertValue
public <T> void assertValue(String content, Matcher<T> matcher)
Evaluate the JSON path expression against the suppliedcontentand assert the resulting value with the givenMatcher.- Parameters:
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.- Parameters:
content- the JSON contentmatcher- the matcher with which to assert the resulttargetType- a the expected type of the resulting value- Since:
- 4.3.3
assertValue
public void assertValue(String content, Object expectedValue)
Evaluate the JSON path expression against the suppliedcontentand assert that the result is equal to the expected value.- Parameters:
content- the JSON contentexpectedValue- the expected value
assertValueIsString
public void assertValueIsString(String content)
Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aString.- Parameters:
content- the JSON content- Since:
- 4.2.1
assertValueIsBoolean
public void assertValueIsBoolean(String content)
Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aBoolean.- Parameters:
content- the JSON content- Since:
- 4.2.1
assertValueIsNumber
public void assertValueIsNumber(String content)
Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aNumber.- Parameters:
content- the JSON content- Since:
- 4.2.1
assertValueIsArray
public void assertValueIsArray(String content)
Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is an array.- Parameters:
content- the JSON content
assertValueIsMap
public void assertValueIsMap(String content)
Evaluate the JSON path expression against the suppliedcontentand assert that the resulting value is aMap.- Parameters:
content- the JSON content- Since:
- 4.2.1
exists
public void exists(String content)
Evaluate the JSON path expression against the suppliedcontentand 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.
- Parameters:
content- the JSON content
doesNotExist
public void doesNotExist(String content)
Evaluate the JSON path expression against the suppliedcontentand 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.
- Parameters:
content- the JSON content
assertValueIsEmpty
public void assertValueIsEmpty(String content)
Evaluate the JSON path expression against the suppliedcontentand 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
public void assertValueIsNotEmpty(String content)
Evaluate the JSON path expression against the suppliedcontentand 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