junit-team/junit5 · error · ExtensionConfigurationException

Can only resolve @TempDir <target> of type java.nio.file.Pat

Error message

Can only resolve @TempDir <target> of type java.nio.file.Path or java.io.File but was: <type>

What it means

Thrown by TempDirectory.assertSupportedType() when a @TempDir-annotated field or parameter has a type other than java.nio.file.Path or java.io.File. The TempDirectory extension only knows how to inject these two types, so any other type (String, InputStream, custom) is rejected as an ExtensionConfigurationException during field injection or parameter resolution.

Source

Thrown at junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java:210

	}

	private TempDirFactory determineTempDirFactory(TempDir tempDir) {
		Class<? extends TempDirFactory> factory = tempDir.factory();

		return factory == TempDirFactory.class //
				? this.configuration.getDefaultTempDirFactorySupplier().get()
				: ReflectionSupport.newInstance(factory);
	}

	private static void assertNonFinalField(Field field) {
		if (ModifierSupport.isFinal(field)) {
			throw new ExtensionConfigurationException("@TempDir field [" + field + "] must not be declared as final.");
		}
	}

	private static void assertSupportedType(String target, Class<?> type) {
		if (type != Path.class && type != File.class) {
			throw new ExtensionConfigurationException("Can only resolve @TempDir " + target + " of type "
					+ Path.class.getName() + " or " + File.class.getName() + " but was: " + type.getName());
		}
	}

	private Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext,
			ExtensionContext extensionContext, TempDir tempDir) {
		TempDirFactory factory = determineTempDirFactory(tempDir);
		Cleanup cleanup = new Cleanup(determineCleanupMode(tempDir), determineDeletionStrategy(tempDir));
		return getPathOrFile(elementType, elementContext, factory, cleanup, extensionContext);
	}

	private static Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext,
			TempDirFactory factory, Cleanup cleanup, ExtensionContext extensionContext) {

		Path path = extensionContext.getStore(NAMESPACE.append(elementContext)) //
				.computeIfAbsent(KEY,
					__ -> createTempDir(factory, cleanup, elementType, elementContext, extensionContext),
					CloseablePath.class) //

View on GitHub (pinned to 956246301e)

Solutions

  1. Change the field/parameter type to java.nio.file.Path (preferred) or java.io.File.
  2. If you need a different representation, keep @TempDir on a Path parameter and convert it inside the test.

Example fix

// before
@TempDir String tmp; // unsupported type
// after
@TempDir java.nio.file.Path tmp;
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate @TempDir field/param type before tests run
if (!annotatedType.equals(Path.class) && !annotatedType.equals(File.class)) {
    throw new IllegalStateException("@TempDir only supports Path or File, got " + annotatedType);
}

Type guard

static boolean isSupportedTempDirType(Class<?> type) {
    return type == java.nio.file.Path.class || type == java.io.File.class;
}

Prevention

When it happens

Trigger: Annotating a field or parameter of an unsupported type with @TempDir — e.g. @TempDir String dir, @TempDir InputStream in, @TempDir MyCustomPathType path.

Common situations: Assuming @TempDir works like a generic path injection and using String; refactoring a Path field to a wrapper type; copy-pasting @TempDir onto the wrong parameter.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/ec8725748705dcd3.json. Report an issue: GitHub.