Class JsonFluentAssert

  • Direct Known Subclasses:
    JsonFluentAssert.ConfigurableJsonFluentAssert

    public class JsonFluentAssert
    extends Object
    Contains JSON related fluent assertions inspired by FEST or AssertJ. Typical usage is: assertThatJson("{\"test\":1}").isEqualTo("{\"test\":2}"); assertThatJson("{\"test\":1}").hasSameStructureAs("{\"test\":21}"); assertThatJson("{\"root\":{\"test\":1}}").node("root.test").isEqualTo("2"); Please note that the method name is assertThatJson and not assertThat. The reason is that we need to accept String parameter and do not want to override standard FEST or AssertJ assertThat(String) method. All the methods accept Objects as parameters. The supported types are:
    1. Jackson JsonNode
    2. Gson JsonElement
    3. Numbers, booleans and any other type parsable by underlying JSON library
    4. String is parsed as JSON. For expected values the string is quoted if it contains obviously invalid JSON.
    5. Reader similarly to String
    6. null as null Node
    • Method Detail

      • isEqualTo

        public JsonFluentAssert isEqualTo​(Object expected)
        Compares JSON for equality. The expected object is converted to JSON before comparison. Ignores order of sibling nodes and whitespaces. Please note that if you pass a String, it's parsed as JSON which can lead to an unexpected behavior. If you pass in "1" it is parsed as a JSON containing integer 1. If you compare it with a string it fails due to a different type. If you want to pass in real string you have to quote it "\"1\"" or use isStringEqualTo(String). If the string parameter is not a valid JSON, it is quoted automatically.
        Parameters:
        expected -
        Returns:
        this object.
        See Also:
        isStringEqualTo(String)
      • isStringEqualTo

        public JsonFluentAssert isStringEqualTo​(String expected)
        Fails if the selected JSON is not a String or is not present or the value is not equal to expected value.
      • isNotEqualTo

        public JsonFluentAssert isNotEqualTo​(Object expected)
        Fails if compared documents are equal. The expected object is converted to JSON before comparison. Ignores order of sibling nodes and whitespaces.
        Parameters:
        expected -
        Returns:
        this object.
      • hasSameStructureAs

        @Deprecated
        public JsonFluentAssert hasSameStructureAs​(Object expected)
        Deprecated.
        Use IGNORING_VALUES option instead
        Compares JSON structure. Ignores values, only compares shape of the document and key names. Is too lenient, ignores types, prefer IGNORING_VALUES option instead.
        Parameters:
        expected -
        Returns:
        this object.
      • node

        public JsonFluentAssert node​(String newPath)
        Creates an assert object that only compares given node. The path is denoted by JSON path, for example. assertThatJson("{\"root\":{\"test\":[1,2,3]}}").node("root.test[0]").isEqualTo("1");
        Parameters:
        newPath -
        Returns:
        object comparing only node given by path.
      • isAbsent

        public JsonFluentAssert isAbsent()
        Fails if the node exists.
        Returns:
      • isObject

        public void isObject()
        Fails if the selected JSON is not an Object or is not present.
      • isString

        public void isString()
        Fails if the selected JSON is not a String or is not present.
      • matches

        public JsonFluentAssert matches​(org.hamcrest.Matcher<?> matcher)
        Matches the node using Hamcrest matcher.
        • Numbers are mapped to BigDecimal
        • Arrays are mapped to a Collection
        • Objects are mapped to a map so you can use json(Part)Equals or a Map matcher
        Parameters:
        matcher -
        Returns: