junit-team/junit5 · error · IllegalStateException
Unsupported AnnotatedElement type for @TempDir: %s
Error message
Unsupported AnnotatedElement type for @TempDir: %s
What it means
IllegalStateException from the internal TempDirDeletionStrategy.IgnoreFailures.descriptionFor(AnnotatedElement) helper, which only knows how to describe Fields and Parameters. If the AnnotatedElement passed in is neither (e.g. a Class, Method, Constructor, or Module), the method cannot produce a human-readable description and throws. It is an internal (@API INTERNAL) safeguard indicating an unsupported element context reached the @TempDir cleanup path.
Source
Thrown at junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDirDeletionStrategy.java:139
return DeletionResult.builder(tempDir).build();
}
private void logWarning(AnnotatedElementContext elementContext, DeletionException exception) {
logger.warn(exception, () -> "Failed to delete all temporary files for %s".formatted(
descriptionFor(elementContext.getAnnotatedElement())));
}
@API(status = INTERNAL, since = "6.1")
public static String descriptionFor(AnnotatedElement annotatedElement) {
if (annotatedElement instanceof Field field) {
return "field " + field.getDeclaringClass().getSimpleName() + "." + field.getName();
}
if (annotatedElement instanceof Parameter parameter) {
Executable executable = parameter.getDeclaringExecutable();
return "parameter '" + parameter.getName() + "' in " + descriptionFor(executable);
}
throw new IllegalStateException("Unsupported AnnotatedElement type for @TempDir: " + annotatedElement);
}
private static String descriptionFor(Executable executable) {
boolean isConstructor = executable instanceof Constructor<?>;
String type = isConstructor ? "constructor" : "method";
String name = isConstructor ? executable.getDeclaringClass().getSimpleName() : executable.getName();
return "%s %s(%s)".formatted(type, name,
ClassUtils.nullSafeToString(Class::getSimpleName, executable.getParameterTypes()));
}
}
/**
* Standard {@link TempDirDeletionStrategy} implementation that recursively
* deletes all files and directories within the temporary directory.
*
* <p>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.View on GitHub (pinned to f070c699a0)
Solutions
- If you wrote a custom AnnotatedElementContext, ensure getAnnotatedElement() returns the actual Field or Parameter annotated with @TempDir.
- Do not reuse the internal IgnoreFailures.descriptionFor helper for arbitrary element types; build your own description string.
- File a JUnit issue if this occurs with stock @TempDir field/parameter injection — it indicates a framework bug.
Example fix
// before: custom context returns a Method
public AnnotatedElement getAnnotatedElement() { return method; }
// after
public AnnotatedElement getAnnotatedElement() { return parameter; } Defensive patterns
Strategy: type-guard
Validate before calling
AnnotatedElement ae = ctx.getAnnotatedElement();
if (!(ae instanceof Field || ae instanceof Parameter)) {
throw new IllegalArgumentException("Unsupported element: " + ae);
} Type guard
static boolean isSupportedTempDirElement(AnnotatedElement ae) {
return ae instanceof Field || ae instanceof Parameter;
} Prevention
- Custom AnnotatedElementContext must return the actual @TempDir Field or Parameter.
- Don't reuse the internal descriptionFor helper for arbitrary element types.
- Report framework bugs if stock injection triggers this.
When it happens
Trigger: An extension or custom TempDirDeletionStrategy calls descriptionFor(...) with an AnnotatedElement that is not a java.lang.reflect.Field or java.lang.reflect.Parameter. Normal @TempDir usage (field or parameter injection) never hits this; it requires an unusual element-context implementation.
Common situations: Writing a custom TempDirDeletionStrategy / AnnotatedElementContext that returns a Method or Class as its annotated element; integration code that reuses the internal descriptionFor helper; an edge case where a constructor-level element is misclassified.
Related errors
- @TempDir field [%s] must not be declared as final.
- Can only resolve @TempDir %s of type java.nio.file.Path or j
- Failed to create default temp directory
- temp directory must be a directory
- temp directory with non-default file system cannot be inject
AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11).
Data as JSON: /api/errors/1fb1d39dc7010add.
Report an issue: GitHub.