Class SnapshotTestContext


  • @API(status=INTERNAL,
         since="1.1.0")
    public final class SnapshotTestContext
    extends java.lang.Object
    Context object that pertains to the execution of a whole test class which is annotated with EnableSnapshotTests.

    In order to initiate the run of a test class which uses snapshot assertions, you need to meet two requirements:

    1. You need to obtain a SnapshotConfiguration instance from the resp. test class via SnapshotConfiguration.defaultConfigurationFor(Class). You can then create a SnapshotTestContext instance from that configuration.
    2. You need to call the lifecycle methods of the context object.
    Most aspects of snapshot testing will already work if you just call finalizeSnapshotTest() after a test method. However, in order to support full orphan detection capabilities, you need to register both all ignored tests and all failed tests of the current test execution.
    Since:
    1.1.0
    • Method Detail

      • testFrameworkSupport

        @API(status=INTERNAL,
             since="1.10.0")
        public TestFrameworkSupport testFrameworkSupport()
        Returns the TestFrameworkSupport that encapsulates test framework specific behavior.
        Returns:
        Thes TestFrameworkSupport.
        Since:
        1.10.0
      • isSnapshotParameter

        public boolean isSnapshotParameter​(java.lang.Class<?> type)
        Determines whether the parameters with the given type are eligible for injecting the object that is created by createSnapshotTestFor(Method).
        Parameters:
        type - A type.
        Returns:
        Whether the type of the object returned by createSnapshotTestFor(Method) is compatible to the given type.
      • createSnapshotTestFor

        public Snapshot createSnapshotTestFor​(java.lang.reflect.Method testMethod)
        Creates a Snapshot object that can be injected into a test method as starting point of the snapshot DSL.

        This method changes the state of this context object. A new Snapshot can only be created, when the current one has been retrieved and cleared using finalizeSnapshotTest().

        Parameters:
        testMethod - The test method.
        Returns:
        A Snapshot instance.
        See Also:
        finalizeSnapshotTest()
      • finalizeSnapshotTest

        public void finalizeSnapshotTest()
                                  throws java.lang.Exception
        Finalizes the current test by clearing this context and executing its remaining assertions.
        Throws:
        java.lang.Exception - If late assertions of the test fail.
        See Also:
        createSnapshotTestFor(Method)
      • recordFailedOrSkippedTest

        public void recordFailedOrSkippedTest​(java.lang.reflect.Method testMethod)
        Records a failed or skipped test within the currently executed test class. Knowing which tests did not complete successfully is crucial for implementing orphaned snapshot detection.
        Parameters:
        testMethod - Test method that failed or has been skipped.
      • detectOrCleanupOrphanedSnapshots

        public java.util.Collection<java.nio.file.Path> detectOrCleanupOrphanedSnapshots()
        Uses the collected context information to detect and optionally also clean up orphaned snapshot files.
        Returns:
        The detected orphans.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object