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, Stephane Nicoll
  • Constructor Details

    • JsonPathExpectationsHelper

      public JsonPathExpectationsHelper(String expression)
      Construct a new JsonPathExpectationsHelper using the default configuration.
      Parameters:
      expression - the JsonPath expression; never null or empty
      Since:
      6.2
    • JsonPathExpectationsHelper

      public JsonPathExpectationsHelper(String expression, @Nullable com.jayway.jsonpath.Configuration configuration)
      Construct a new JsonPathExpectationsHelper.
      Parameters:
      expression - the JsonPath expression; never null or empty
      configuration - the Configuration to use or null to use the default configuration
      Since:
      6.2
    • JsonPathExpectationsHelper

      @Deprecated(since="6.2", forRemoval=true) public JsonPathExpectationsHelper(String expression, Object... args)
      Deprecated, for removal: This API element is subject to removal in a future version.
      in favor of calling String.formatted(Object...) upfront
      Construct a new JsonPathExpectationsHelper.
      Parameters:
      expression - the JsonPath expression; never null or empty
      args - arguments to parameterize the JsonPath expression with, using formatting specifiers defined in String.format(String, Object...)
  • Method Details

    • assertValue

      public <T> void assertValue(String content, Matcher<? super T> matcher)
      Evaluate the JSON path expression against the supplied content and assert the resulting value with the given Matcher.
      Parameters:
      content - the JSON content
      matcher - the matcher with which to assert the result
    • assertValue

      public <T> void assertValue(String content, Matcher<? super T> matcher, Class<T> targetType)
      An overloaded variant of assertValue(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 content
      matcher - the matcher with which to assert the result
      targetType - the expected type of the resulting value
      Since:
      4.3.3
    • assertValue

      public <T> void assertValue(String content, Matcher<? super T> matcher, ParameterizedTypeReference<T> targetType)
      An overloaded variant of assertValue(String, Matcher) that also accepts a target type for the resulting value that allows generic types to be defined.

      This must be used with a Configuration that defines a more elaborate MappingProvider as the default one cannot handle generic types.

      Parameters:
      content - the JSON content
      matcher - the matcher with which to assert the result
      targetType - the expected type of the resulting value
      Since:
      6.2
    • assertValue

      public void assertValue(String content, @Nullable Object expectedValue)
      Evaluate the JSON path expression against the supplied content and assert that the result is equal to the expected value.
      Parameters:
      content - the JSON content
      expectedValue - the expected value
    • assertValueIsString

      public void assertValueIsString(String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a String.
      Parameters:
      content - the JSON content
      Since:
      4.2.1
    • assertValueIsBoolean

      public void assertValueIsBoolean(String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Boolean.
      Parameters:
      content - the JSON content
      Since:
      4.2.1
    • assertValueIsNumber

      public void assertValueIsNumber(String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Number.
      Parameters:
      content - the JSON content
      Since:
      4.2.1
    • assertValueIsArray

      public void assertValueIsArray(String content)
      Evaluate the JSON path expression against the supplied content and 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 supplied content and assert that the resulting value is a Map.
      Parameters:
      content - the JSON content
      Since:
      4.2.1
    • exists

      public void exists(String content)
      Evaluate the JSON path expression against the supplied content 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

      public void doesNotExist(String content)
      Evaluate the JSON path expression against the supplied content 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

      public void assertValueIsEmpty(String content)
      Evaluate the JSON path expression against the supplied content 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

      public void assertValueIsNotEmpty(String content)
      Evaluate the JSON path expression against the supplied 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).

      Parameters:
      content - the JSON content
    • hasJsonPath

      public void hasJsonPath(String content)
      Evaluate the JSON path expression against the supplied content and assert that a value, possibly null, 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

      public void doesNotHaveJsonPath(String content)
      Evaluate the JSON path expression against the supplied content and assert that a value, including null 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

      @Nullable public Object evaluateJsonPath(String content)
      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

      public <T> T evaluateJsonPath(String content, Class<T> targetType)
      Variant of evaluateJsonPath(String) with a target type.

      This can be useful for matching numbers reliably for example coercing an integer into a double or when the configured MappingProvider can handle more complex object structures.

      Parameters:
      content - the content to evaluate against
      targetType - the requested target type
      Returns:
      the result of the evaluation
      Throws:
      AssertionError - if the evaluation fails
    • evaluateJsonPath

      public <T> T evaluateJsonPath(String content, ParameterizedTypeReference<T> targetType)
      Variant of evaluateJsonPath(String) with a target type that has generics.

      This must be used with a Configuration that defines a more elaborate MappingProvider as the default one cannot handle generic types.

      Parameters:
      content - the content to evaluate against
      targetType - the requested target type
      Returns:
      the result of the evaluation
      Throws:
      AssertionError - if the evaluation fails
      Since:
      6.2