{"id":"ec8725748705dcd3","repo":"junit-team/junit5","slug":"can-only-resolve-tempdir-target-of-type-java-ni","errorCode":null,"errorMessage":"Can only resolve @TempDir <target> of type java.nio.file.Path or java.io.File but was: <type>","messagePattern":"Can only resolve @TempDir <target> of type java\\.nio\\.file\\.Path or java\\.io\\.File but was: <type>","errorType":"exception","errorClass":"ExtensionConfigurationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java","lineNumber":210,"sourceCode":"\t}\n\n\tprivate TempDirFactory determineTempDirFactory(TempDir tempDir) {\n\t\tClass<? extends TempDirFactory> factory = tempDir.factory();\n\n\t\treturn factory == TempDirFactory.class //\n\t\t\t\t? this.configuration.getDefaultTempDirFactorySupplier().get()\n\t\t\t\t: ReflectionSupport.newInstance(factory);\n\t}\n\n\tprivate static void assertNonFinalField(Field field) {\n\t\tif (ModifierSupport.isFinal(field)) {\n\t\t\tthrow new ExtensionConfigurationException(\"@TempDir field [\" + field + \"] must not be declared as final.\");\n\t\t}\n\t}\n\n\tprivate static void assertSupportedType(String target, Class<?> type) {\n\t\tif (type != Path.class && type != File.class) {\n\t\t\tthrow new ExtensionConfigurationException(\"Can only resolve @TempDir \" + target + \" of type \"\n\t\t\t\t\t+ Path.class.getName() + \" or \" + File.class.getName() + \" but was: \" + type.getName());\n\t\t}\n\t}\n\n\tprivate Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext,\n\t\t\tExtensionContext extensionContext, TempDir tempDir) {\n\t\tTempDirFactory factory = determineTempDirFactory(tempDir);\n\t\tCleanup cleanup = new Cleanup(determineCleanupMode(tempDir), determineDeletionStrategy(tempDir));\n\t\treturn getPathOrFile(elementType, elementContext, factory, cleanup, extensionContext);\n\t}\n\n\tprivate static Object getPathOrFile(Class<?> elementType, AnnotatedElementContext elementContext,\n\t\t\tTempDirFactory factory, Cleanup cleanup, ExtensionContext extensionContext) {\n\n\t\tPath path = extensionContext.getStore(NAMESPACE.append(elementContext)) //\n\t\t\t\t.computeIfAbsent(KEY,\n\t\t\t\t\t__ -> createTempDir(factory, cleanup, elementType, elementContext, extensionContext),\n\t\t\t\t\tCloseablePath.class) //","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java#L192-L228","documentation":"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.","triggerScenarios":"Annotating a field or parameter of an unsupported type with @TempDir — e.g. @TempDir String dir, @TempDir InputStream in, @TempDir MyCustomPathType path.","commonSituations":"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.","solutions":["Change the field/parameter type to java.nio.file.Path (preferred) or java.io.File.","If you need a different representation, keep @TempDir on a Path parameter and convert it inside the test."],"exampleFix":"// before\n@TempDir String tmp; // unsupported type\n// after\n@TempDir java.nio.file.Path tmp;","handlingStrategy":"type-guard","validationCode":"// Validate @TempDir field/param type before tests run\nif (!annotatedType.equals(Path.class) && !annotatedType.equals(File.class)) {\n    throw new IllegalStateException(\"@TempDir only supports Path or File, got \" + annotatedType);\n}","typeGuard":"static boolean isSupportedTempDirType(Class<?> type) {\n    return type == java.nio.file.Path.class || type == java.io.File.class;\n}","tryCatchPattern":null,"preventionTips":["Use java.nio.file.Path (preferred) or java.io.File for @TempDir.","Convert to other representations inside the test body, not via the annotation."],"tags":["junit-jupiter","tempdir","extension","type-mismatch"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}