Class TempDirectory
- java.lang.Object
-
- org.junit.jupiter.api.support.io.TempDirectory
-
- All Implemented Interfaces:
Extension
,ParameterResolver
@API(status=EXPERIMENTAL, since="5.4") public final class TempDirectory extends Object implements 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 notPath
or if the temporary directory could not be created, this extension will throw aParameterResolutionException
.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 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, e.g. 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
TempDir
can be used to annotate a test or lifecycle method or test class constructor parameter of typePath
that should be resolved into a temporary directory.
-
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 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 an argument for theParameter
in the suppliedParameterContext
for the suppliedExtensionContext
.boolean
supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Determine if this resolver supports resolution of an argument for theParameter
in the suppliedParameterContext
for the suppliedExtensionContext
.
-
-
-
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
-
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
- used to configure the parent directory for the temporary directories created by this extension- Returns:
- a
TempDirectory
extension
-
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
- used to configure the parent directory for the temporary directories created by this extension
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Description copied from interface:ParameterResolver
Determine if this resolver supports resolution of an argument for theParameter
in the suppliedParameterContext
for the suppliedExtensionContext
.The
Method
orConstructor
in which the parameter is declared can be retrieved viaParameterContext.getDeclaringExecutable()
.- 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)
Description copied from interface:ParameterResolver
Resolve an argument for theParameter
in the suppliedParameterContext
for the suppliedExtensionContext
.This method is only called by the framework if
ParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
previously returnedtrue
for the sameParameterContext
andExtensionContext
.The
Method
orConstructor
in which the parameter is declared can be retrieved viaParameterContext.getDeclaringExecutable()
.- 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
-
-