{"record":{"id":"b65cc4cdf12d9807","repo":"junit-team/junit5","slug":"can-only-resolve-tempdir-s-of-type-java-nio-file","errorCode":null,"errorMessage":"Can only resolve @TempDir %s of type java.nio.file.Path or java.io.File but was: %s","messagePattern":"Can only resolve @TempDir (.+?) of type java\\.nio\\.file\\.Path or java\\.io\\.File but was: (.+?)","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/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java#L192-L228","documentation":"Thrown as ExtensionConfigurationException by 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 concrete types and refuses anything else at setup time.","triggerScenarios":"Annotating a field/parameter typed as String, File[], Path[], InputStream, or any custom type with @TempDir. assertSupportedType(target, type) checks `type != Path.class && type != File.class` and throws.","commonSituations":"Confusing @TempDir with @TempDir factory APIs; migrating from JUnit 4 TemporaryFolder and typing the field as a Folder-like wrapper; declaring a generic Path subtype; Kotlin type alias that resolves to something else.","solutions":["Change the @TempDir field/parameter type to java.nio.file.Path (preferred) or java.io.File.","If you need a richer abstraction, inject Path and build your wrapper object in @BeforeEach from it."],"exampleFix":"// before\n@TempDir\nprivate String tmpPath; // wrong type\n\n// after\n@TempDir\nprivate Path tmpPath;\n\n@BeforeEach\nvoid setup() { this.wrapper = new FolderWrapper(tmpPath); }","handlingStrategy":"type-guard","validationCode":"for (Field f : MyTest.class.getDeclaredFields()) {\n    if (f.isAnnotationPresent(TempDir.class)) {\n        Class<?> t = f.getType();\n        if (t != Path.class && t != File.class) {\n            throw new IllegalStateException(\"@TempDir \" + f + \" must be Path or File, was \" + t);\n        }\n    }\n}","typeGuard":"boolean isTempDirType(Class<?> t) {\n    return t == Path.class || t == File.class;\n}","tryCatchPattern":null,"preventionTips":["Type @TempDir fields and parameters as java.nio.file.Path (preferred) or java.io.File.","Build richer wrappers in @BeforeEach from the injected Path instead of typing the field differently.","Avoid type aliases that resolve to non-Path/File types."],"tags":["tempdir","extension","type-mismatch","field-injection"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}