Class TextSnapshot

  • All Implemented Interfaces:
    StructuredDataProvider

    @API(status=STABLE)
    public final class TextSnapshot
    extends java.lang.Object
    implements StructuredDataProvider
    Take snapshots using Object.toString(). By default, whitespace changes of any kind will be ignored during comparison. This behavior can be changed using withIgnoreWhitespaces(boolean).

    When rendering unified diffs as String, changes in line separators are not marked at every occurrence. Instead, detected changes in line separators will result in a single informative message at the beginning of the diff, but only if set withIgnoreWhitespaces(boolean) to false.

    By default, rendered diffs will display 5 lines of context around a detected change. This can be changed using withContextLines(int). Note that SnapshotTestOptions.textDiffContextLines() does not apply to text snapshots.

    • Field Detail

      • text

        public static final StructuredData text
        Take Snapshots using Object.toString() and compare the results using a generic String diff algorithm. Comparison ignores whitespace changes of any kind.

        You can create a more customized text comparison by using text() and the several customization options.

        See Also:
        text()
    • Method Detail

      • text

        @API(status=STABLE,
             since="1.4.0")
        public static TextSnapshot text()
        Creates a customizable TextSnapshot instance. You can also use the static constant text when you want to use only defaults.
        Returns:
        The customizable text snapshot StructuredDataProvider.
        Since:
        1.4.0
      • withIgnoreWhitespaces

        @API(status=STABLE,
             since="1.4.0")
        public TextSnapshot withIgnoreWhitespaces​(boolean ignoreWhitespaces)
        Allows to customize the whitespace comparison behavior.

        Defaults to false.

        Parameters:
        ignoreWhitespaces - Whether to ignore whitespaces during comparison.
        Returns:
        This instance.
      • withContextLines

        @API(status=EXPERIMENTAL,
             since="1.5.0")
        public TextSnapshot withContextLines​(int contextLines)
        Configures the amount of contextual lines that are printed around a detected change. Per default, all lines of the full diff will be printed.

        Defaults to 5

        Parameters:
        contextLines - The amount of lines to print around a detected change in the unified diffs.
        Returns:
        This instance.
        Since:
        1.5.0
      • withDiffFormat

        @API(status=EXPERIMENTAL,
             since="1.7.0")
        public TextSnapshot withDiffFormat​(TextSnapshot.DiffFormat diffFormat)
        Specify the format of how diffs are rendered within our assertion failure messages.

        Defaults to TextSnapshot.DiffFormat.UNIFIED

        Parameters:
        diffFormat - The diff format to use.
        Returns:
        This instance.
        Since:
        1.7.0