Class TempDirectory
- java.lang.Object
-
- org.junit.jupiter.api.support.io.TempDirectory
-
- All Implemented Interfaces:
BeforeAllCallback
,BeforeEachCallback
,Extension
,ParameterResolver
@API(status=EXPERIMENTAL, since="5.4") public final class TempDirectory extends 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 neitherPath
norFile
or if the temporary directory could not be created, this extension will throw anExtensionConfigurationException
or aParameterResolutionException
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 astatic
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 thecreateInCustomDirectory(ParentDirProvider)
orcreateInCustomDirectory(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-partyFileSystem
implementation — for example, Jimfs.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TempDirectory.ParentDirProvider
ParentDirProvider
can be used to configure a custom parent directory for all temporary directories created by theTempDirectory
extension this is used with.static interface
TempDirectory.TempDir
static interface
TempDirectory.TempDirContext
TempDirContext
encapsulates the context in which@TempDir
is declared.
-
Constructor Summary
Constructors Constructor Description TempDirectory()
Create a newTempDirectory
extension that uses the defaultFileSystem
and creates temporary directories in the default location.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beforeAll(ExtensionContext context)
void
beforeEach(ExtensionContext context)
static TempDirectory
createInCustomDirectory(Callable<Path> parentDirProvider)
Returns aTempDirectory
extension that uses the suppliedCallable
to configure the parent directory for the temporary directories created by this extension.static TempDirectory
createInCustomDirectory(TempDirectory.ParentDirProvider parentDirProvider)
Create aTempDirectory
extension that uses the suppliedTempDirectory.ParentDirProvider
to configure the parent directory for the temporary directories created by this extension.static TempDirectory
createInDefaultDirectory()
Create aTempDirectory
extension that uses the defaultFileSystem
and creates temporary directories in the default location.Object
resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve the current temporary directory for theParameter
in the suppliedParameterContext
.boolean
supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
-
-
-
Constructor Detail
-
TempDirectory
public TempDirectory()
Create a newTempDirectory
extension that uses the defaultFileSystem
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 aTempDirectory
extension that uses the defaultFileSystem
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; nevernull
-
createInCustomDirectory
public static TempDirectory createInCustomDirectory(TempDirectory.ParentDirProvider parentDirProvider)
Create aTempDirectory
extension that uses the suppliedTempDirectory.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
- aParentDirProvider
used to configure the parent directory for the temporary directories created by this extension; nevernull
- Returns:
- a
TempDirectory
extension; nevernull
-
createInCustomDirectory
public static TempDirectory createInCustomDirectory(Callable<Path> parentDirProvider)
Returns aTempDirectory
extension that uses the suppliedCallable
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
- aCallable
that returns aPath
used to configure the parent directory for the temporary directories created by this extension; nevernull
- Returns:
- a
TempDirectory
extension; nevernull
-
beforeAll
public void beforeAll(ExtensionContext context) throws Exception
Perform field injection for non-private,static
fields (i.e., class fields) of typePath
orFile
that are annotated with@TempDir
.- Specified by:
beforeAll
in interfaceBeforeAllCallback
- Parameters:
context
- the current extension context; nevernull
- Throws:
Exception
-
beforeEach
public void beforeEach(ExtensionContext context) throws Exception
Perform field injection for non-private, non-static fields (i.e., instance fields) of typePath
orFile
that are annotated with@TempDir
.- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Parameters:
context
- the current extension context; nevernull
- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
supportsParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which an argument should be resolved; nevernull
extensionContext
- the extension context for theExecutable
about to be invoked; nevernull
- Returns:
true
if this resolver can resolve an argument for the parameter- See Also:
ParameterResolver.resolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,ParameterContext
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve the current temporary directory for theParameter
in the suppliedParameterContext
.- Specified by:
resolveParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which an argument should be resolved; nevernull
extensionContext
- the extension context for theExecutable
about to be invoked; nevernull
- Returns:
- the resolved argument for the parameter; may only be
null
if the parameter type is not a primitive - See Also:
ParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,ParameterContext
-
-