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
TempDirectoryis 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 notPathor if the temporary directory could not be created, this extension will throw aParameterResolutionException.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 a parameter of a@BeforeAllmethod or the test class constructor. Otherwise, e.g. when only used on test or@BeforeEachor@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 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, e.g. Jimfs.
-
-
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.TempDirTempDircan be used to annotate a test or lifecycle method or test class constructor parameter of typePaththat should be resolved into a temporary directory.
-
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 static TempDirectorycreateInCustomDirectory(Callable<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.ObjectresolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)Resolve an argument for theParameterin the suppliedParameterContextfor the suppliedExtensionContext.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)Determine if this resolver supports resolution of an argument for theParameterin the suppliedParameterContextfor the suppliedExtensionContext.
-
-
-
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
-
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- used to configure the parent directory for the temporary directories created by this extension- Returns:
- a
TempDirectoryextension
-
createInCustomDirectory
public static TempDirectory createInCustomDirectory(Callable<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- 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:ParameterResolverDetermine if this resolver supports resolution of an argument for theParameterin the suppliedParameterContextfor the suppliedExtensionContext.The
MethodorConstructorin which the parameter is declared can be retrieved viaParameterContext.getDeclaringExecutable().- 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 Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Description copied from interface:ParameterResolverResolve an argument for theParameterin the suppliedParameterContextfor 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 returnedtruefor the sameParameterContextandExtensionContext.The
MethodorConstructorin which the parameter is declared can be retrieved viaParameterContext.getDeclaringExecutable().- 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
-
-