class TempDirectory extends java.lang.Object implements BeforeAllCallback, BeforeEachCallback, ParameterResolver
TempDirectory
is a JUnit Jupiter extension that creates and cleans
up temporary directories if field in a test class or a parameter in a
lifecycle method or test method is annotated with @TempDir
.
Consult the Javadoc for TempDir
for details on the contract.
TempDir
,
Files.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute<?>...)
Modifier and Type | Class and Description |
---|---|
private static class |
TempDirectory.CloseablePath |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
KEY |
private static ExtensionContext.Namespace |
NAMESPACE |
private static java.lang.String |
TEMP_DIR_PREFIX |
Constructor and Description |
---|
TempDirectory() |
Modifier and Type | Method and Description |
---|---|
private void |
assertSupportedType(java.lang.String target,
java.lang.Class<?> type) |
private void |
assertValidFieldCandidate(java.lang.reflect.Field field) |
void |
beforeAll(ExtensionContext context)
Perform field injection for non-private,
static fields (i.e.,
class fields) of type Path or File that are annotated with
@TempDir . |
void |
beforeEach(ExtensionContext context)
Perform field injection for non-private, non-static fields (i.e.,
instance fields) of type
Path or File that are annotated
with @TempDir . |
private static TempDirectory.CloseablePath |
createTempDir() |
private java.lang.Object |
getPathOrFile(java.lang.Class<?> type,
ExtensionContext extensionContext) |
private void |
injectFields(ExtensionContext context,
java.lang.Object testInstance,
java.util.function.Predicate<java.lang.reflect.Field> predicate) |
java.lang.Object |
resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
Resolve the current temporary directory for the
Parameter in the
supplied ParameterContext . |
boolean |
supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
|
private static final ExtensionContext.Namespace NAMESPACE
private static final java.lang.String KEY
private static final java.lang.String TEMP_DIR_PREFIX
public void beforeAll(ExtensionContext context) throws java.lang.Exception
static
fields (i.e.,
class fields) of type Path
or File
that are annotated with
@TempDir
.beforeAll
in interface BeforeAllCallback
context
- the current extension context; never null
java.lang.Exception
public void beforeEach(ExtensionContext context) throws java.lang.Exception
Path
or File
that are annotated
with @TempDir
.beforeEach
in interface BeforeEachCallback
context
- the current extension context; never null
java.lang.Exception
private void injectFields(ExtensionContext context, java.lang.Object testInstance, java.util.function.Predicate<java.lang.reflect.Field> predicate)
private void assertValidFieldCandidate(java.lang.reflect.Field field)
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
supportsParameter
in interface ParameterResolver
parameterContext
- the context for the parameter for which an argument should
be resolved; never null
extensionContext
- the extension context for the Executable
about to be invoked; never null
true
if this resolver can resolve an argument for the parameterParameterResolver.resolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,
ParameterContext
public java.lang.Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Parameter
in the
supplied ParameterContext
.resolveParameter
in interface ParameterResolver
parameterContext
- the context for the parameter for which an argument should
be resolved; never null
extensionContext
- the extension context for the Executable
about to be invoked; never null
null
if the
parameter type is not a primitiveParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,
ParameterContext
private void assertSupportedType(java.lang.String target, java.lang.Class<?> type)
private java.lang.Object getPathOrFile(java.lang.Class<?> type, ExtensionContext extensionContext)
private static TempDirectory.CloseablePath createTempDir()