junit-team/junit5 · error · PreconditionViolationException

temp directory with non-default file system cannot be…

Error message

temp directory with non-default file system cannot be injected into java.io.File target

What it means

Thrown as PreconditionViolationException inside the CloseablePath constructor when the injected element type is java.io.File but the created temp directory lives on a non-default FileSystem. A Path from a custom FileSystem cannot be losslessly converted to a java.io.File (which is tied to the default file system), so injection is refused and the path is closed first.

Solutions

  1. Change the @TempDir target type from java.io.File to java.nio.file.Path so the non-default FileSystem is supported.
  2. Or configure the custom factory to create the directory on the default FileSystem (FileSystems.getDefault()) when a File target is required.
  3. Adapt your test code to operate on Path rather than File when using in-memory file systems.

Example fix

// before
@TempDir(factory = JimfsFactory.class)
File dir; // File + non-default FS -> rejected

// after - switch to Path
@TempDir(factory = JimfsFactory.class)
Path dir;
Defensive patterns

Strategy: type-guard

Validate before calling

Path dir = factory.createTempDirectory(ctx, ec);
boolean defaultFs = dir.getFileSystem().equals(FileSystems.getDefault());
boolean wantsFile = elementType == File.class;
if (wantsFile && !defaultFs) {
    throw new IllegalStateException("Non-default FS cannot be injected into File; use Path");
}

Type guard

Class<?> elementType = /* resolved element type */;
boolean isPathTarget = elementType == Path.class;
boolean isFileTarget = elementType == File.class;
boolean defaultFs = Files.getDefaultFileSystem().equals(...);
if (isFileTarget && !defaultFs) {
    // must switch to Path
}

Prevention

When it happens

Trigger: CloseablePath constructor: `elementType == File.class && !this.dir.getFileSystem().equals(FileSystems.getDefault())`. Happens when a custom TempDirFactory creates the directory on an in-memory or zip FileSystem (e.g. Jimfs) but the @TempDir target is typed File.

Common situations: Using Google Jimfs or a custom FileSystem for test isolation and asking JUnit to inject java.io.File; copy-paste of a Path-based test into a File-based field while keeping the custom in-memory factory; memory file system tests.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/f0566d82f510cf42. Report an issue: GitHub.

Appendix: 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 f070c699a0)