Class JsonSnapshot

  • All Implemented Interfaces:
    StructuredDataProvider

    @API(status=STABLE)
    public final class JsonSnapshot
    extends Object
    implements StructuredDataProvider
    Serializes snapshots as json using jackson. Use either the static factory methods or the ready-to-use static instance json. You can either provide an explicit ObjectMapper or use one with sensible defaults.

    Either way, the ObjectMapper can be further configured by calling configure(Consumer).

    Warning: It is discouraged to use any of this builder's methods after build() has been called. Further modifications to this builder might be reflected in any objects that have been built earlier. Later API versions might enforce this suggestion by throwing an exception.

    Author:
    Simon Taddiken
    • Field Detail

      • json

        public static final StructuredDataProvider json
        Takes Snapshots using jackson ObjectMapper and compare the results using JSONAssert.
    • Method Detail

      • withDefaultObjectMapper

        @Deprecated(since="1.4.0",
                    forRemoval=true)
        @API(status=DEPRECATED,
             since="1.4.0")
        public static JsonSnapshot withDefaultObjectMapper()
        Deprecated, for removal: This API element is subject to removal in a future version.
        Since 1.4.0 - Use json() instead.
        Creates an instance using a default ObjectMapper with sensible defaults. The object mapper can be configured further using configure(Consumer).
        Returns:
        A builder for building StructuredData.
      • json

        public static JsonSnapshot json()
        Creates an instance using a default ObjectMapper with sensible defaults. The object mapper can be configured further using configure(Consumer).
        Returns:
        A builder for building StructuredData.
      • json

        public static JsonSnapshot json​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
        Creates an instance using the explicitly provided ObjectMapper.
        Parameters:
        objectMapper - The ObjectMapper to use for taking snapshots.
        Returns:
        A builder for building StructuredData.
      • withObjectMapper

        @Deprecated(since="1.4.0",
                    forRemoval=true)
        @API(status=DEPRECATED,
             since="1.4.0")
        public static JsonSnapshot withObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Since 1.4.0 - Use json(ObjectMapper) instead.
        Creates an instance using the explicitly provided ObjectMapper.
        Parameters:
        objectMapper - The ObjectMapper to use for taking snapshots.
        Returns:
        A builder for building StructuredData.
      • configure

        public JsonSnapshot configure​(Consumer<com.fasterxml.jackson.databind.ObjectMapper> c)
        Configure the underlying ObjectMapper by passing in a Consumer. Can be used to tweak the default ObjectMapper.

        The passed Consumer will be called immediately to modify the object mapper in place. That is either the default object mapper or the one that has been passed to json(ObjectMapper) during construction of this object.

        Parameters:
        c - The consumer to which the ObjectMapper will be passed.
        Returns:
        This instance.
      • withComparator

        @Deprecated(forRemoval=true,
                    since="1.2.0")
        @API(status=DEPRECATED,
             since="1.2.0")
        public JsonSnapshot withComparator​(org.skyscreamer.jsonassert.comparator.JSONComparator jsonComparator)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Since 1.2.0 - Use withComparisonRules(Consumer) instead.
        Sets the JSONComparator for comparing actual and expected. If not set, the DefaultComparator along with JSONCompareMode.STRICT will be used.

        Note: this method can not be used in combination with withComparisonRules(Consumer) as they will internally overwrite their respective configuration.

        Parameters:
        jsonComparator - The comparator to use.
        Returns:
        This instance.
        API Note:
        This method is likely going to be replaced with a wrapper API around JSONAssert types. The JSONAssert API is a bit awkward to use and I'd like to remove it as a visible dependency altogether
      • withCompareMode

        @API(status=STABLE,
             since="1.5.0")
        public JsonSnapshot withCompareMode​(CompareMode compareMode)
        Sets the mode for comparing two json strings. Defaults to CompareMode.STRICT.
        Parameters:
        compareMode - The compare mode to use.
        Returns:
        This instance.
      • withComparisonRules

        @API(status=EXPERIMENTAL,
             since="1.2.0")
        public JsonSnapshot withComparisonRules​(Consumer<ComparisonRuleBuilder> rules)
        Allows to specify extra comparison rules that are applied to certain paths within the json snapshots.

        Note: this method can not be used in combination with withComparator(JSONComparator) as they will internally overwrite their respective configuration.

        Parameters:
        rules - A consumer to which a ComparisonRuleBuilder will be passed.
        Returns:
        This instance.