Class TempDirectory

  • All Implemented Interfaces:
    BeforeAllCallback, BeforeEachCallback, Extension, ParameterResolver

    @API(status=EXPERIMENTAL,
         since="5.4")
    public final class TempDirectory
    extends java.lang.Object
    implements BeforeAllCallback, BeforeEachCallback, ParameterResolver
    TempDirectory is a JUnit Jupiter extension that creates and cleans up temporary directories.

    The temporary directory is only created if a field in a test class or a parameter in a test method, lifecycle method, or test class constructor is annotated with @TempDir. If the field or parameter type is neither Path nor File or if the temporary directory could not be created, this extension will throw an ExtensionConfigurationException or a ParameterResolutionException as appropriate.

    The scope of the temporary directory depends on where the first @TempDir annotation is encountered when executing a test class. The temporary directory will be shared by all tests in a class when the annotation is present on a static field, on a parameter of a @BeforeAll method, or on a parameter of the test class constructor. Otherwise — for example, when only used on test, @BeforeEach, or @AfterEach methods — each test will use its own temporary directory.

    When the end of the scope of a temporary directory is reached, i.e. when the test method or class has finished execution, this extension will attempt to recursively delete all files and directories in the temporary directory and, finally, the temporary directory itself. In case deletion of a file or directory fails, this extension will throw an IOException that will cause the test or test class to fail.

    By default, this extension will use the default FileSystem to create temporary directories in the default location. However, you may instantiate this extension using the createInCustomDirectory(ParentDirProvider) or createInCustomDirectory(Callable) factory methods and register it via @RegisterExtension to pass a custom provider to configure the parent directory for all temporary directories created by this extension. This allows the use of this extension with any third-party FileSystem implementation — for example, Jimfs.

    Since:
    5.4
    See Also:
    TempDirectory.TempDir, TempDirectory.ParentDirProvider, Files.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute<?>...)
    • Constructor Detail

      • TempDirectory

        public TempDirectory()
        Create a new TempDirectory extension that uses the default FileSystem and creates temporary directories in the default location.

        This constructor is used by the JUnit Jupiter Engine when the extension is registered via @ExtendWith.

    • Method Detail

      • createInDefaultDirectory

        public static TempDirectory createInDefaultDirectory()
        Create a TempDirectory extension that uses the default FileSystem and creates temporary directories in the default location.

        You may use this factory method when registering this extension via @RegisterExtension, although you might prefer the simpler registration via @ExtendWith.

        Returns:
        a TempDirectory extension; never null
      • createInCustomDirectory

        public static TempDirectory createInCustomDirectory​(TempDirectory.ParentDirProvider parentDirProvider)
        Create a TempDirectory extension that uses the supplied TempDirectory.ParentDirProvider to configure the parent directory for the temporary directories created by this extension.

        You may use this factory method when registering this extension via @RegisterExtension.

        Parameters:
        parentDirProvider - a ParentDirProvider used to configure the parent directory for the temporary directories created by this extension; never null
        Returns:
        a TempDirectory extension; never null
      • createInCustomDirectory

        public static TempDirectory createInCustomDirectory​(java.util.concurrent.Callable<java.nio.file.Path> parentDirProvider)
        Returns a TempDirectory extension that uses the supplied Callable to configure the parent directory for the temporary directories created by this extension.

        You may use this factory method when registering this extension via @RegisterExtension.

        Parameters:
        parentDirProvider - a Callable that returns a Path used to configure the parent directory for the temporary directories created by this extension; never null
        Returns:
        a TempDirectory extension; never null
      • beforeAll

        public void beforeAll​(ExtensionContext context)
                       throws java.lang.Exception
        Perform field injection for non-private, static fields (i.e., class fields) of type Path or File that are annotated with @TempDir.
        Specified by:
        beforeAll in interface BeforeAllCallback
        Parameters:
        context - the current extension context; never null
        Throws:
        java.lang.Exception
      • beforeEach

        public void beforeEach​(ExtensionContext context)
                        throws java.lang.Exception
        Perform field injection for non-private, non-static fields (i.e., instance fields) of type Path or File that are annotated with @TempDir.
        Specified by:
        beforeEach in interface BeforeEachCallback
        Parameters:
        context - the current extension context; never null
        Throws:
        java.lang.Exception