junit-team/junit5 · error · ConversionException
Failed to convert String "%s" to type java.net.URL
Error message
Failed to convert String "%s" to type java.net.URL
What it means
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.
Source
Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java:60
@Override
public boolean canConvertTo(Class<?> targetType) {
return CONVERTERS.containsKey(targetType);
}
@Override
public Object convert(String source, Class<?> targetType) throws Exception {
Function<String, ?> converter = Preconditions.notNull(CONVERTERS.get(targetType),
() -> "No registered converter for %s".formatted(targetType.getName()));
return converter.apply(source);
}
private static URL toURL(String url) {
try {
return URI.create(url).toURL();
}
catch (MalformedURLException ex) {
throw new ConversionException("Failed to convert String \"" + url + "\" to type java.net.URL", ex);
}
}
}
View on GitHub (pinned to f070c699a0)
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
Example fix
// before
@ParameterizedTest
@ValueSource(strings = "localhost:8080")
void test(URL u) { ... }
// after
@ParameterizedTest
@ValueSource(strings = "http://localhost:8080")
void test(URL u) { ... } Defensive patterns
Strategy: validation
Validate before calling
try {
URI.create(source).toURL(); // pre-flight: will this convert cleanly?
} catch (MalformedURLException e) {
// fix the input or fall back to URI
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: @ValueSource/@CsvSource feeding bare hostnames ('localhost'), unsupported schemes, or malformed URLs into a URL parameter; tests assuming URL accepts the same syntax as URI.
Related errors
- Cannot convert null to primitive value of type %s
- Failed to convert String "%s" to type %s
- No built-in converter for source type java.lang.String and t
- Configuration error: You must configure at least one set of
- Cannot convert to %s: %s
AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11).
Data as JSON: /api/errors/4a3fb8afe77a3bab.
Report an issue: GitHub.