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 java.lang.Object implements BeforeAllCallback, BeforeEachCallback, ParameterResolverTempDirectoryis 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 neitherPathnorFileor if the temporary directory could not be created, this extension will throw anExtensionConfigurationExceptionor aParameterResolutionExceptionas appropriate.The scope of the temporary directory depends on where the first
@TempDirannotation 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 astaticfield, on a parameter of a@BeforeAllmethod, or on a parameter of the test class constructor. Otherwise — for example, when only used on test,@BeforeEach, or@AfterEachmethods — 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
IOExceptionthat will cause the test or test class to fail.By default, this extension will use the default
FileSystemto create temporary directories in the default location. However, you may instantiate this extension using thecreateInCustomDirectory(ParentDirProvider)orcreateInCustomDirectory(Callable)factory methods and register it via@RegisterExtensionto 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-partyFileSystemimplementation — 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<?>...)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTempDirectory.ParentDirProviderParentDirProvidercan be used to configure a custom parent directory for all temporary directories created by theTempDirectoryextension this is used with.static interfaceTempDirectory.TempDir@TempDircan be used to annotate a field in a test class or a parameter in a test method, lifecycle method, or test class constructor of typePathorFilethat should be resolved into a temporary directory.static interfaceTempDirectory.TempDirContextTempDirContextencapsulates the context in which@TempDiris declared.
-
Constructor Summary
Constructors Constructor Description TempDirectory()Create a newTempDirectoryextension that uses the defaultFileSystemand creates temporary directories in the default location.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeforeAll(ExtensionContext context)Perform field injection for non-private,staticfields (i.e., class fields) of typePathorFilethat are annotated with@TempDir.voidbeforeEach(ExtensionContext context)Perform field injection for non-private, non-static fields (i.e., instance fields) of typePathorFilethat are annotated with@TempDir.static TempDirectorycreateInCustomDirectory(java.util.concurrent.Callable<java.nio.file.Path> parentDirProvider)Returns aTempDirectoryextension that uses the suppliedCallableto configure the parent directory for the temporary directories created by this extension.static TempDirectorycreateInCustomDirectory(TempDirectory.ParentDirProvider parentDirProvider)Create aTempDirectoryextension that uses the suppliedTempDirectory.ParentDirProviderto configure the parent directory for the temporary directories created by this extension.static TempDirectorycreateInDefaultDirectory()Create aTempDirectoryextension that uses the defaultFileSystemand creates temporary directories in the default location.java.lang.ObjectresolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)Resolve the current temporary directory for theParameterin the suppliedParameterContext.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
-
-
-
Constructor Detail
-
TempDirectory
public TempDirectory()
Create a newTempDirectoryextension that uses the defaultFileSystemand 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 aTempDirectoryextension that uses the defaultFileSystemand 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
TempDirectoryextension; nevernull
-
createInCustomDirectory
public static TempDirectory createInCustomDirectory(TempDirectory.ParentDirProvider parentDirProvider)
Create aTempDirectoryextension that uses the suppliedTempDirectory.ParentDirProviderto 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- aParentDirProviderused to configure the parent directory for the temporary directories created by this extension; nevernull- Returns:
- a
TempDirectoryextension; nevernull
-
createInCustomDirectory
public static TempDirectory createInCustomDirectory(java.util.concurrent.Callable<java.nio.file.Path> parentDirProvider)
Returns aTempDirectoryextension that uses the suppliedCallableto 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- aCallablethat returns aPathused to configure the parent directory for the temporary directories created by this extension; nevernull- Returns:
- a
TempDirectoryextension; nevernull
-
beforeAll
public void beforeAll(ExtensionContext context) throws java.lang.Exception
Perform field injection for non-private,staticfields (i.e., class fields) of typePathorFilethat are annotated with@TempDir.- Specified by:
beforeAllin interfaceBeforeAllCallback- Parameters:
context- the current extension context; nevernull- 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 typePathorFilethat are annotated with@TempDir.- Specified by:
beforeEachin interfaceBeforeEachCallback- Parameters:
context- the current extension context; nevernull- Throws:
java.lang.Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
supportsParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter for which an argument should be resolved; nevernullextensionContext- the extension context for theExecutableabout to be invoked; nevernull- Returns:
trueif 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 java.lang.Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve the current temporary directory for theParameterin the suppliedParameterContext.- Specified by:
resolveParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter for which an argument should be resolved; nevernullextensionContext- the extension context for theExecutableabout to be invoked; nevernull- Returns:
- the resolved argument for the parameter; may only be
nullif the parameter type is not a primitive - See Also:
ParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext),ParameterContext
-
-