junit-team/junit5 · error · PreconditionViolationException
temp directory with non-default file system cannot be inject
Error message
temp directory with non-default file system cannot be injected into java.io.File target
What it means
Thrown by CloseablePath's constructor when the TempDirFactory returns a Path whose FileSystem is not the default filesystem, but the injection target type is java.io.File. java.io.File can only represent paths on the default filesystem, so converting a non-default Path to File would be lossy/incorrect; the constructor rejects it as a PreconditionViolationException after closing the factory.
Source
Thrown at junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java:278
private final AnnotatedElementContext elementContext;
private final ExtensionContext extensionContext;
private CloseablePath(TempDirFactory factory, Cleanup cleanup, Class<?> elementType,
AnnotatedElementContext elementContext, ExtensionContext extensionContext) throws Exception {
this.dir = factory.createTempDirectory(elementContext, extensionContext);
this.factory = factory;
this.cleanup = cleanup;
this.elementContext = elementContext;
this.extensionContext = extensionContext;
if (this.dir == null || !Files.isDirectory(this.dir)) {
close();
throw new PreconditionViolationException("temp directory must be a directory");
}
if (elementType == File.class && !this.dir.getFileSystem().equals(FileSystems.getDefault())) {
close();
throw new PreconditionViolationException(
"temp directory with non-default file system cannot be injected into " + File.class.getName()
+ " target");
}
}
Path get() {
return requireNonNull(this.dir);
}
@Override
public void close() throws IOException {
try {
if (this.dir != null) {
this.cleanup.run(this.dir, this.elementContext, this.extensionContext);
}
}
finally {
this.factory.close();View on GitHub (pinned to 956246301e)
Solutions
- Change the @TempDir field/parameter type from java.io.File to java.nio.file.Path so a non-default filesystem is supported.
- Or make the custom TempDirFactory create the directory on the default filesystem (FileSystems.getDefault()) if a File target is required.
Example fix
// before @TempDir(factory = JimfsFactory.class) File tmp; // Jimfs path is non-default fs -> PreconditionViolationException // after @TempDir(factory = JimfsFactory.class) java.nio.file.Path tmp; // Path supports any FileSystem
Defensive patterns
Strategy: type-guard
Validate before calling
// If using a non-default-fs factory, force the target type to Path
if (usesCustomFileSystem(factory) && targetType == File.class) {
throw new IllegalStateException("Use Path, not File, with a non-default filesystem factory");
} Type guard
static boolean targetMatchesFileSystem(Class<?> targetType, Path sample) {
return targetType == java.nio.file.Path.class
|| java.nio.file.FileSystems.getDefault().equals(sample.getFileSystem());
} Prevention
- When using Jimfs or zip-filesystem factories, declare @TempDir as Path, never File.
- If a File target is mandatory, ensure the factory creates dirs on the default filesystem.
- Document the factory's filesystem expectation on its Javadoc.
When it happens
Trigger: Using a custom @TempDir(factory = ...) that returns a Path from a custom FileSystem (e.g. Jimfs in-memory filesystem, zip filesystem), while declaring the @TempDir field/parameter as java.io.File rather than java.nio.file.Path.
Common situations: In-memory filesystem testing libraries (Jimfs) wired as a TempDirFactory, with a test field accidentally typed as File; refactoring a Path-based test to File while keeping a custom factory that produces non-default-fs paths.
Related errors
- Can only resolve @TempDir <target> of type java.nio.file.Pat
- Failed to create default temp directory
- temp directory must be a directory
- TestInstanceFactory [%s] failed to return an instance of [%s
- @TempDir field [<field>] must not be declared as final.
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/f0566d82f510cf42.json.
Report an issue: GitHub.