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

  1. Use a fully-qualified URL string with a supported scheme (http, https, file, jar)
  2. Switch the parameter type to URI.class if the value is a valid URI but not a valid URL
  3. 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

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


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/4a3fb8afe77a3bab. Report an issue: GitHub.