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
    Construct a new JsonPathExpectationsHelper.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    assertValue(String content, Object expectedValue)
    Evaluate the JSON path expression against the supplied content 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 supplied content and assert the resulting value with the given Matcher.
    <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.
    void
    Evaluate the JSON path expression against the supplied content and assert that the resulting value is an array.
    void
    Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Boolean.
    void
    Evaluate the JSON path expression against the supplied content and assert that an empty value exists at the given path.
    void
    Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Map.
    void
    Evaluate the JSON path expression against the supplied content and assert that a non-empty value exists at the given path.
    void
    Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Number.
    void
    Evaluate the JSON path expression against the supplied content and assert that the resulting value is a String.
    void
    Evaluate the JSON path expression against the supplied content and assert that a non-null value does not exist at the given path.
    void
    Evaluate the JSON path expression against the supplied content and assert that a value, including null values, does not exist at the given path.
    Evaluate the JSON path and return the resulting value.
    evaluateJsonPath(String content, Class<?> targetType)
    Variant of evaluateJsonPath(String) with a target type.
    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.
    void
    Evaluate the JSON path expression against the supplied content and assert that a value, possibly null, exists.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonPathExpectationsHelper

      public JsonPathExpectationsHelper(String expression, Object... args)
      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 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 Object evaluateJsonPath(String content, Class<?> 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.

      Parameters:
      content - the content to evaluate against
      Returns:
      the result of the evaluation
      Throws:
      AssertionError - if the evaluation fails