Annotation Type TempDir
-
@Target({FIELD,PARAMETER}) @Retention(RUNTIME) @Documented @API(status=EXPERIMENTAL, since="5.4") public @interface TempDir@TempDircan be used to annotate a non-private field in a test class or a parameter in a lifecycle method or test method of typePathorFilethat should be resolved into a temporary directory.Please note that
@TempDiris not supported on constructor parameters. Please use field injection instead, by annotating a non-private instance field with@TempDir.Temporary Directory Creation
The temporary directory is only created if a field in a test class or a parameter in a lifecycle method or test method is annotated with
@TempDir. If the field type or parameter type is neitherPathnorFileor if the temporary directory cannot be created, anExtensionConfigurationExceptionor aParameterResolutionExceptionwill be thrown as appropriate. In addition, aParameterResolutionExceptionwill be thrown for a constructor parameter annotated with@TempDir.Temporary Directory Scope
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 or on a parameter of a@BeforeAllmethod. Otherwise — for example, when@TempDiris only used on instance fields or on parameters in test,@BeforeEach, or@AfterEachmethods — each test will use its own temporary directory.Temporary Directory Deletion
When the end of the scope of a temporary directory is reached, i.e. when the test method or class has finished execution, JUnit 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, an
IOExceptionwill be thrown that will cause the test or test class to fail.- Since:
- 5.4