Annotation Type EnableSnapshotTests


  • @Retention(RUNTIME)
    @Target({TYPE,METHOD})
    @ExtendWith(SnapshotExtension.class)
    @API(status=STABLE)
    public @interface EnableSnapshotTests
    Enables the snapshot-test capabilities. When you mark a class with this annotation, you can use snapshot assertions by declaring a parameter of type SnapshotDsl.Snapshot in your test case like this:
     @EnableSnapshotTests
     class MyTestClass {
    
         @Test
         void testSomething(Snapshot snapshot) throws Exception {
             Object actual = ...
             snapshot.assertThat(actual).asText().matchesSnapshotText();
         }
     }
     

    asText() will 'serialize' actual test results using Object.toString(). There are additional StructuredDataProvider implementations that allow to serialize snapshots as json or xml. To use them, you need to declare their respective maven modules as dependency.

         @Test
         void testSomething(Snapshot snapshot) throws Exception {
             Object actual = ...
             snapshot.assertThat(actual).as(TextSnapshot.text).matchesSnapshotText();
             snapshot.assertThat(actual).as(JsonSnapshot.json).matchesSnapshotText();
             snapshot.assertThat(actual).as(XmlSnapshot.xml).matchesSnapshotText();
         }
     

    When providing a structured data format like json/xml (or in general: an implementation of StructuredDataProvider) you can make use of structural assertions to compare snapshots. Depending on the implementation, those might provide better error messages than plain text comparison.

         @Test
         void testSomething(Snapshot snapshot) throws Exception {
             Object actual = ...
             snapshot.assertThat(actual).as(JsonSnapshot.json).matchesSnapshotStructure();
             snapshot.assertThat(actual).as(XmlSnapshot.xml).matchesSnapshotStructure();
         }
     
    Author:
    Simon Taddiken
    See Also:
    SnapshotDsl.Snapshot
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      boolean forceUpdateSnapshots
      Can be set to true temporarily in order to force to update the persisted snapshots with the current test results.
      String snapshotDirectory
      Define the snapshot directory relative to src/test/resources.
      boolean softAssertions
      When enabled, a test method using snapshot assertions will continue to execute, even if a snapshot assertion failed.
    • Element Detail

      • snapshotDirectory

        String snapshotDirectory
        Define the snapshot directory relative to src/test/resources. If this is not defined, snapshots will be stored in a directory structure according to the package name of the test class.
        Returns:
        The relative directory to store the snapshots.
        Default:
        ""
      • forceUpdateSnapshots

        boolean forceUpdateSnapshots
        Can be set to true temporarily in order to force to update the persisted snapshots with the current test results.

        Warning: While this is attribute is set to true, all tests containing snapshot assertions will fail with an error. This is to prevent accidentally checking in disabled assertions.

        After snapshots have been updated, you should reset this flag to false and run the tests again before checking your code into any SCM.

        Returns:
        Whether to update the stored snapshots.
        See Also:
        SnapshotDsl.ChooseAssertions.justUpdateSnapshot()
        Default:
        false
      • softAssertions

        boolean softAssertions
        When enabled, a test method using snapshot assertions will continue to execute, even if a snapshot assertion failed. This allows to collect multiple failing snapshots with a single test execution.

        The failures from all snapshot comparisons within the single test methods will be collected and reported after the test method completed.

        Returns:
        Whether to enable soft assertions. Defaults to false.
        Default:
        false