{"record":{"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/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java#L117-L150","documentation":"Generic wrapper thrown when a registered StringToObjectConverter.convert raises something other than a ConversionException (which is rethrown unchanged at line 130-132). The original exception is attached as the cause so the caller can see the real failure (NumberFormatException, DateTimeParseException, IllegalArgumentException, ...).","triggerScenarios":"ConversionSupport.convert(\"abc\", Integer.class, loader) where the numeric parser throws NumberFormatException; convert(\"2024-13-99\", LocalDate.class, loader) where the date parser throws DateTimeParseException; convert(\"NOPE\", MyEnum.class, loader) where the enum lookup fails.","commonSituations":"Malformed numbers in @CsvSource/@ValueSource mapped to numeric types; bad date/time patterns reaching java.time targets; invalid enum names; UUID strings with wrong format; any source value a built-in converter cannot parse.","solutions":["Inspect getCause() on the thrown ConversionException to find the real parse error","Fix the offending input value in the argument source","Add a custom ArgumentConverter via @ConvertWith that handles the input format","Use @CsvFileSource with nullValues / ignoreLeadingAndTrailingWhitespace where appropriate"],"exampleFix":"// before\n@ParameterizedTest\n@ValueSource(strings = \"12a\")\nvoid test(int n) { ... }\n\n// after\n@ParameterizedTest\n@ValueSource(strings = \"12\")\nvoid test(int n) { ... }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    T value = ConversionSupport.convert(source, targetType, classLoader);\n} catch (ConversionException e) {\n    Throwable cause = e.getCause(); // the real parse failure (NumberFormatException, etc.)\n    // handle/log/return default\n}","preventionTips":["Validate argument-source data at suite build time","Always inspect getCause() - the wrapper message only says 'Failed to convert'","Sanitize CSV input before feeding it to parameterized tests"],"tags":["conversion","parameterized-test","input-validation"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}