{"record":{"id":"4a3fb8afe77a3bab","repo":"junit-team/junit5","slug":"failed-to-convert-string-s-to-type-java-net-url","errorCode":null,"errorMessage":"Failed to convert String \"%s\" to type java.net.URL","messagePattern":"Failed to convert String \"(.+?)\" to type java\\.net\\.URL","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java","lineNumber":60,"sourceCode":"\n\t@Override\n\tpublic boolean canConvertTo(Class<?> targetType) {\n\t\treturn CONVERTERS.containsKey(targetType);\n\t}\n\n\t@Override\n\tpublic Object convert(String source, Class<?> targetType) throws Exception {\n\t\tFunction<String, ?> converter = Preconditions.notNull(CONVERTERS.get(targetType),\n\t\t\t() -> \"No registered converter for %s\".formatted(targetType.getName()));\n\t\treturn converter.apply(source);\n\t}\n\n\tprivate static URL toURL(String url) {\n\t\ttry {\n\t\t\treturn URI.create(url).toURL();\n\t\t}\n\t\tcatch (MalformedURLException ex) {\n\t\t\tthrow new ConversionException(\"Failed to convert String \\\"\" + url + \"\\\" to type java.net.URL\", ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":42,"sourceCodeEnd":65,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java#L42-L65","documentation":"Thrown by StringToCommonJavaTypesConverter.toURL when a string parses as a URI (URI.create succeeds) but URI.toURL() throws MalformedURLException. The offending URL string is embedded in the message and the original MalformedURLException is the cause.","triggerScenarios":"ConversionSupport.convert(\"ftp://x\", URL.class, loader), or any string whose scheme is not a valid URL scheme, or a string missing the protocol: URI.create accepts it but toURL() rejects it.","commonSituations":"@ValueSource/@CsvSource feeding bare hostnames ('localhost'), unsupported schemes, or malformed URLs into a URL parameter; tests assuming URL accepts the same syntax as URI.","solutions":["Use a fully-qualified URL string with a supported scheme (http, https, file, jar)","Switch the parameter type to URI.class if the value is a valid URI but not a valid URL","Sanitize or validate the input string before conversion"],"exampleFix":"// before\n@ParameterizedTest\n@ValueSource(strings = \"localhost:8080\")\nvoid test(URL u) { ... }\n\n// after\n@ParameterizedTest\n@ValueSource(strings = \"http://localhost:8080\")\nvoid test(URL u) { ... }","handlingStrategy":"validation","validationCode":"try {\n    URI.create(source).toURL(); // pre-flight: will this convert cleanly?\n} catch (MalformedURLException e) {\n    // fix the input or fall back to URI\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer URI when the input may not be URL-compatible","Always include the scheme in URL argument sources","Validate URLs at data-build time, not at test-execution time"],"tags":["conversion","network","url","parameterized-test"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}