org.springframework.test.util

Class JsonPathExpectationsHelper

  • java.lang.Object
    • org.springframework.test.util.JsonPathExpectationsHelper


  • public class JsonPathExpectationsHelper
    extends java.lang.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 and Description
      JsonPathExpectationsHelper(java.lang.String expression, java.lang.Object... args)
      Construct a new JsonPathExpectationsHelper.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      <T> void assertValue(java.lang.String content, org.hamcrest.Matcher<T> matcher)
      Evaluate the JSON path expression against the supplied content and assert the resulting value with the given Matcher.
      <T> void assertValue(java.lang.String content, org.hamcrest.Matcher<T> matcher, java.lang.Class<T> targetType)
      An overloaded variant of assertValue(String, Matcher) that also accepts a target type for the resulting value.
      void assertValue(java.lang.String content, java.lang.Object expectedValue)
      Evaluate the JSON path expression against the supplied content and assert that the result is equal to the expected value.
      void assertValueIsArray(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is an array.
      void assertValueIsBoolean(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Boolean.
      void assertValueIsEmpty(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that an empty value exists at the given path.
      void assertValueIsMap(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Map.
      void assertValueIsNotEmpty(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that a non-empty value exists at the given path.
      void assertValueIsNumber(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a Number.
      void assertValueIsString(java.lang.String content)
      Evaluate the JSON path expression against the supplied content and assert that the resulting value is a String.
      void doesNotExist(java.lang.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.
      void doesNotHaveJsonPath(java.lang.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.
      java.lang.Object evaluateJsonPath(java.lang.String content)
      Evaluate the JSON path and return the resulting value.
      java.lang.Object evaluateJsonPath(java.lang.String content, java.lang.Class<?> targetType)
      Variant of evaluateJsonPath(String) with a target type.
      void exists(java.lang.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 hasJsonPath(java.lang.String content)
      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 Detail

      • JsonPathExpectationsHelper

        public JsonPathExpectationsHelper(java.lang.String expression,
                                          java.lang.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 Detail

      • assertValue

        public <T> void assertValue(java.lang.String content,
                                    org.hamcrest.Matcher<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(java.lang.String content,
                                    org.hamcrest.Matcher<T> matcher,
                                    java.lang.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 - a the expected type of the resulting value
        Since:
        4.3.3
      • assertValue

        public void assertValue(java.lang.String content,
                                @Nullable
                                java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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 java.lang.Object evaluateJsonPath(java.lang.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:
        java.lang.AssertionError - if the evaluation fails
      • evaluateJsonPath

        public java.lang.Object evaluateJsonPath(java.lang.String content,
                                                 java.lang.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:
        java.lang.AssertionError - if the evaluation fails