{"id":"6f1b4706168a0482","repo":"junit-team/junit5","slug":"failed-to-convert-string-s-to-type-s","errorCode":null,"errorMessage":"Failed to convert String \"%s\" to type %s","messagePattern":"Failed to convert String \"(.+?)\" to type (.+?)","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java","lineNumber":135,"sourceCode":"\t\t\treturn (T) source;\n\t\t}\n\n\t\tClass<?> targetTypeToUse = toWrapperType(targetType);\n\t\tOptional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(\n\t\t\tcandidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();\n\t\tif (converter.isPresent()) {\n\t\t\ttry {\n\t\t\t\tClassLoader classLoaderToUse = classLoader != null ? classLoader\n\t\t\t\t\t\t: ClassLoaderUtils.getDefaultClassLoader();\n\t\t\t\treturn (T) converter.get().convert(source, targetTypeToUse, classLoaderToUse);\n\t\t\t}\n\t\t\tcatch (Exception ex) {\n\t\t\t\tif (ex instanceof ConversionException conversionException) {\n\t\t\t\t\t// simply rethrow it\n\t\t\t\t\tthrow conversionException;\n\t\t\t\t}\n\t\t\t\t// else\n\t\t\t\tthrow new ConversionException(\n\t\t\t\t\t\"Failed to convert String \\\"%s\\\" to type %s\".formatted(source, targetType.getTypeName()), ex);\n\t\t\t}\n\t\t}\n\n\t\tthrow new ConversionException(\n\t\t\t\"No built-in converter for source type java.lang.String and target type \" + targetType.getTypeName());\n\t}\n\n\tprivate static Class<?> toWrapperType(Class<?> targetType) {\n\t\tClass<?> wrapperType = getWrapperType(targetType);\n\t\treturn wrapperType != null ? wrapperType : targetType;\n\t}\n\n}\n","sourceCodeStart":117,"sourceCodeEnd":150,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java#L117-L150","documentation":"ConversionSupport.convert wraps any non-ConversionException thrown by a built-in converter. It means the source string could be routed to a converter for the target type but the converter rejected it - e.g. NumberFormatException converting \"abc\" to Integer.","triggerScenarios":"A @CsvSource / @ValueSource string that the target type's converter cannot parse: \"abc\" for Integer, \"xyz\" for Boolean, a non-ISO date for LocalDate, etc.","commonSituations":"Mismatched CSV data format and parameter type; locale-specific number formats; date strings not matching the ISO-8601 format the converter expects.","solutions":["Align the source string with the converter's parse rules (Integer.parseInt grammar, ISO-8601 for java.time, TRUE/false/0/1 for Boolean).","Register a custom ArgumentConverter via @ConvertWith for non-standard formats.","Accept the parameter as String and parse it inside the test body."],"exampleFix":"// before\n@ParameterizedTest\n@CsvSource({ \"abc\" })\nvoid test(int n) { }\n\n// after\n@ParameterizedTest\n@CsvSource({ \"42\" })\nvoid test(int n) { }","handlingStrategy":"try-catch","validationCode":"// Pre-parse the value the way the built-in converter will.\nString cell = \"42\";\ntry {\n    Integer.parseInt(cell);\n} catch (NumberFormatException ex) {\n    throw new IllegalArgumentException(\"Cell '\" + cell + \"' is not a valid Integer\", ex);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ConversionSupport.convert(source, targetType, null);\n} catch (ConversionException e) {\n    if (e.getMessage().startsWith(\"Failed to convert String\")) {\n        // align the source string format with the converter, or register a custom ArgumentConverter\n    } else throw e;\n}","preventionTips":["Match the source string to the converter's grammar (Integer.parseInt, ISO-8601, etc.).","Provide a custom ArgumentConverter via @ConvertWith for non-standard formats.","Unit-test each value you intend to feed to a parameterized test."],"tags":["junit","platform-commons","conversion","parsing","parameterized-test"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}