Top Description Fields Methods
org.junit.jupiter.api.io

public @Interface TempDir

extends Annotation
Annotations
@Target:ANNOTATION_TYPE, FIELD, PARAMETER
@Retention:RUNTIME
@Documented
@API
status:STABLE
since:5.10
Static Imports
org.apiguardian.api.API.Status.MAINTAINED, .API.Status.STABLE

@TempDir can be used to annotate a field in a test class or a parameter in a test class constructor, lifecycle method, or test method of type Path or File that should be resolved into a temporary directory.

Creation

The temporary directory is only created if a field in a test class or a parameter in a test class constructor, lifecycle method, or test method is annotated with @TempDir. An ExtensionConfigurationException or a ParameterResolutionException will be thrown in one of the following cases:

Scope

By default, a separate temporary directory is created for every declaration of the @TempDir annotation. For better isolation when using @TestInstance(Lifecycle.PER_METHOD) semantics, you can annotate an instance field or a parameter in the test class constructor with @TempDir so that each test method uses a separate temporary directory. Alternatively, if you want to share a temporary directory across all tests in a test class, you should declare the annotation on a static field or on a parameter of a @BeforeAll method.

Clean Up

By default, when the end of the scope of a temporary directory is reached, — when the test method or class has finished execution — JUnit will attempt to clean up the temporary directory by recursively deleting all files and directories in the temporary directory and, finally, the temporary directory itself. Symbolic and other types of links, such as junctions on Windows, are not followed. A warning is logged when deleting a link that targets a location outside the temporary directory. In case deletion of a file or directory fails, an IOException will be thrown that will cause the test or test class to fail.

The cleanup attribute allows you to configure the CleanupMode. If the cleanup mode is set to NEVER, the temporary directory will not be cleaned up after the test completes. If the cleanup mode is set to ON_SUCCESS, the temporary directory will only be cleaned up if the test completes successfully. By default, the ALWAYS clean up mode will be used, but this can be configured globally by setting the junit.jupiter.tempdir.cleanup.mode.default configuration parameter.

Since
5.4

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_CLEANUP_MODE_PROPERTY_NAME

The name of the configuration parameter that is used to configure the default CleanupMode.

public static final String
DEFAULT_FACTORY_PROPERTY_NAME

Property name used to set the default temporary directory factory class name: Supported Values

Supported values include fully qualified class names for types that implement TempDirFactory.

Method Summary

Modifier and TypeMethod and Description
public CleanupMode
cleanup()

How the temporary directory gets cleaned up after the test completes.

public Class<? extends TempDirFactory>

Returns:

the type of TempDirFactory to use
factory
()

Factory for the temporary directory.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Field Detail

DEFAULT_CLEANUP_MODE_PROPERTY_NAMEback to summary
public static final String DEFAULT_CLEANUP_MODE_PROPERTY_NAME

The name of the configuration parameter that is used to configure the default CleanupMode.

If this configuration parameter is not set, CleanupMode#ALWAYS will be used as the default.

Annotations
@API
status:MAINTAINED
since:5.13.3
Since
5.9
DEFAULT_FACTORY_PROPERTY_NAMEback to summary
public static final String DEFAULT_FACTORY_PROPERTY_NAME

Property name used to set the default temporary directory factory class name:

Supported Values

Supported values include fully qualified class names for types that implement TempDirFactory.

If not specified, the default is TempDirFactory.Standard.

Annotations
@API
status:MAINTAINED
since:5.13.3
Since
5.10

Method Detail

cleanupback to summary
public CleanupMode cleanup()

How the temporary directory gets cleaned up after the test completes.

Annotations
@API
status:STABLE
since:5.11
Since
5.9
factoryback to summary
public Class<? extends TempDirFactory> factory()

Factory for the temporary directory.

Defaults to TempDirFactory.Standard.

As an alternative to setting this attribute, a global TempDirFactory can be configured for the entire test suite via the junit.jupiter.tempdir.factory.default configuration parameter. See the User Guide for details. Note, however, that a @TempDir declaration with a custom factory always overrides a global TempDirFactory.

Returns:Class<? extends TempDirFactory>

the type of TempDirFactory to use

Annotations
@API
status:MAINTAINED
since:5.13.3
Since
5.10
See Also
TempDirFactory