public class TempDirectoryExtension
extends java.lang.Object
implements org.junit.jupiter.api.extension.ParameterResolver
TempDirectory
is a JUnit Jupiter extension to create and clean up a
temporary directory.
The temporary directory is only created if a test or lifecycle method or
test class constructor has a parameter annotated with
@TempDir
. If the parameter type is not Path
or if the
temporary directory could not be created, this extension will throw a
ParameterResolutionException
.
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 parameter of a
@BeforeAll
method or the test class
constructor. Otherwise, e.g. when only used on test or
@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 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, e.g.
Jimfs.
Since JUnit Jupiter 5.4, there's a
built-in @TempDir
extension. If you don't need support for
arbitrary file systems, you should consider using that instead of this
extension.
For more details and examples, see
the documentation on TempDirectory
TempDir
,
TempDirectoryExtension.ParentDirProvider
,
Files.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute<?>...)
Modifier and Type | Class and Description |
---|---|
static interface |
TempDirectoryExtension.ParentDirProvider
ParentDirProvider can be used to configure a custom parent
directory for all temporary directories created by the
TempDirectoryExtension extension this is used with. |
Constructor and Description |
---|
TempDirectoryExtension()
Create a new
TempDirectory extension that uses the default
FileSystem and creates temporary
directories in the default location. |
Modifier and Type | Method and Description |
---|---|
static TempDirectoryExtension |
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. |
static TempDirectoryExtension |
createInCustomDirectory(TempDirectoryExtension.ParentDirProvider parentDirProvider)
Returns a
TempDirectory extension that uses the supplied
TempDirectoryExtension.ParentDirProvider to configure the parent directory for the
temporary directories created by this extension. |
static TempDirectoryExtension |
createInDefaultDirectory()
Returns a
TempDirectory extension that uses the default
FileSystem and creates temporary
directories in the default location. |
java.lang.Object |
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
boolean |
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
public TempDirectoryExtension()
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
.
public static TempDirectoryExtension createInDefaultDirectory()
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
.
TempDirectory
extensionpublic static TempDirectoryExtension createInCustomDirectory(TempDirectoryExtension.ParentDirProvider parentDirProvider)
TempDirectory
extension that uses the supplied
TempDirectoryExtension.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
.
parentDirProvider
- used to configure the parent directory for the
temporary directories created by this extensionpublic static TempDirectoryExtension createInCustomDirectory(java.util.concurrent.Callable<java.nio.file.Path> parentDirProvider)
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
.
parentDirProvider
- used to configure the parent directory for the
temporary directories created by this extensionpublic boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
supportsParameter
in interface org.junit.jupiter.api.extension.ParameterResolver
public java.lang.Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
resolveParameter
in interface org.junit.jupiter.api.extension.ParameterResolver