Class SnapshotTesting


  • public class SnapshotTesting
    extends Object
    Test file content and directory tree to make sure they are valid by comparing them to their snapshots. The snapshots files can easily be updated when necessary and reviewed to confirm they are consistent with the changes.

    The snapshots files will be created/updated using -Dsnap or -Dupdate-snapshots
    Snapshots are created in SNAPSHOTS_DIR
    • Field Detail

      • SNAPSHOTS_DIR

        public static final Path SNAPSHOTS_DIR
    • Constructor Detail

      • SnapshotTesting

        public SnapshotTesting()
    • Method Detail

      • getSnapshotsBaseTree

        public static io.quarkus.paths.PathTree getSnapshotsBaseTree()
      • withSnapshotsDir

        public static <T> T withSnapshotsDir​(String relativePath,
                                             Function<Path,​T> function)
      • assertThatMatchSnapshot

        public static org.assertj.core.api.AbstractPathAssert<?> assertThatMatchSnapshot​(org.junit.jupiter.api.TestInfo testInfo,
                                                                                         Path parentDir,
                                                                                         String fileRelativePath)
                                                                                  throws Throwable
        Test file content to make sure it is valid by comparing it to its snapshots.
        The snapshot file can easily be updated when necessary and reviewed to confirm it is consistent with the changes.

        The snapshot file will be created/updated using -Dsnap or -Dupdate-snapshots

        Even if the content is checked as a whole, it's always better to also manually check that specific content snippets contains what's expected

        example:
         assertThatMatchSnapshot(testInfo, projectDir, "src/main/java/org/acme/GreetingResource.java")
                 .satisfies(checkContains("@Path(\"/hello\")"))
         
        Parameters:
        testInfo - the TestInfo from the parameter (used to get the current test class & method to compute the snapshot location)
        parentDir - the parent directory containing the generated files for this test (makes it nicer when checking multiple snapshots)
        fileRelativePath - the relative path from the directory (used to name the snapshot)
        Returns:
        an AbstractPathAssert giving a direct way to check specific content snippets contains what's expected
        Throws:
        Throwable
      • assertThatMatchSnapshot

        public static org.assertj.core.api.AbstractPathAssert<?> assertThatMatchSnapshot​(Path fileToCheck,
                                                                                         String snapshotIdentifier)
                                                                                  throws Throwable
        Test file content to make sure it is valid by comparing it to a snapshot.
        The snapshot file can easily be updated when necessary and reviewed to confirm it is consistent with the changes.

        The snapshot file will be created/updated using -Dsnap or -Dupdate-snapshots

        Even if the content is checked as a whole, it's always better to also manually check that specific content snippets contains what's expected using checkContains(String) or checkMatches(String)
        Parameters:
        fileToCheck - the Path of the file to check
        snapshotIdentifier - the snapshotIdentifier of the snapshot (used as a relative path from the SNAPSHOTS_DIR
        Returns:
        an AbstractPathAssert giving a direct way to check specific content snippets contains what's expected
        Throws:
        Throwable
      • assertThatDirectoryTreeMatchSnapshots

        public static org.assertj.core.api.ListAssert<String> assertThatDirectoryTreeMatchSnapshots​(org.junit.jupiter.api.TestInfo testInfo,
                                                                                                    Path dir)
                                                                                             throws Throwable
        Test directory tree to make sure it is valid by comparing it to a snapshot.
        The snapshot file can easily be updated when necessary and reviewed to confirm it is consistent with the changes.

        The snapshot file will be created/updated using -Dsnap or -Dupdate-snapshots
        Parameters:
        testInfo - the TestInfo from the parameter (used to get the current test class & method to compute the snapshot location)
        dir - the Path of the directory to test
        Returns:
        a ListAssert with the directory tree as a list
        Throws:
        Throwable
      • assertThatDirectoryTreeMatchSnapshots

        public static org.assertj.core.api.ListAssert<String> assertThatDirectoryTreeMatchSnapshots​(String snapshotDirName,
                                                                                                    Path dir)
                                                                                             throws Throwable
        Test directory tree to make sure it is valid by comparing it to a snapshot.
        The snapshot file can easily be updated when necessary and reviewed to confirm it is consistent with the changes.

        The snapshot file will be created/updated using -Dsnap or -Dupdate-snapshots
        Parameters:
        snapshotDirName - the snapshot dir name for storage
        dir - the Path of the directory to test
        Returns:
        a ListAssert with the directory tree as a list
        Throws:
        Throwable
      • getTextContent

        public static String getTextContent​(Path file)
      • checkContains

        public static Consumer<Path> checkContains​(String s)
        To use with AbstractPathAssert in order to check the file content contains a specific string.
        Parameters:
        s - the string which should be in the file content
        Returns:
        a Consumer to use with AbstractPathAssert#satisfies(Consumer)
      • getSnapshotDirName

        public static String getSnapshotDirName​(org.junit.jupiter.api.TestInfo testInfo)
      • normalizePathAsName

        public static String normalizePathAsName​(String fileRelativePath)