junit-team/junit5 · error · PreconditionViolationException
Failed to create a java.net.URI from: ${uri}
Error message
Failed to create a java.net.URI from: ${uri} What it means
Thrown by DiscoverySelectors.selectUri(String) (line 80-88) when 'new URI(uri)' raises URISyntaxException. It is wrapped in a PreconditionViolationException. The blank-string guard runs first (Preconditions.notBlank), so this specifically means the string is non-blank but syntactically invalid per java.net.URI's strict RFC parsing.
Source
Thrown at junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java:86
/**
* Create a {@code UriSelector} for the supplied URI.
*
* @param uri the URI to select; never {@code null} or blank
* @see UriSelector
* @see #selectUri(URI)
* @see #selectFile(String)
* @see #selectFile(File)
* @see #selectDirectory(String)
* @see #selectDirectory(File)
*/
public static UriSelector selectUri(String uri) {
Preconditions.notBlank(uri, "URI must not be null or blank");
try {
return new UriSelector(new URI(uri));
}
catch (URISyntaxException ex) {
throw new PreconditionViolationException("Failed to create a java.net.URI from: " + uri, ex);
}
}
/**
* Create a {@code UriSelector} for the supplied {@link URI}.
*
* @param uri the URI to select; never {@code null}
* @see UriSelector
* @see #selectUri(String)
* @see #selectFile(String)
* @see #selectFile(File)
* @see #selectDirectory(String)
* @see #selectDirectory(File)
*/
public static UriSelector selectUri(URI uri) {
Preconditions.notNull(uri, "URI must not be null");
return new UriSelector(uri);
}View on GitHub (pinned to 956246301e)
Solutions
- Pre-construct a URI with java.net.URI or Path.toUri() and pass it to selectUri(URI) to avoid re-parsing.
- Percent-encode the string (URLEncoder / URI multi-arg constructor) before passing to selectUri(String).
- For local files use DiscoverySelectors.selectFile(path) instead of a hand-built file:// URI.
- Validate first: new URI(s) in a try/catch and log the exact URISyntaxException index/message.
Example fix
// before
var selector = DiscoverySelectors.selectUri("C:\\Users\\me\\tests\\data.xml"); // backslashes -> throws
// after
URI uri = Path.of("C:\\Users\\me\\tests\\data.xml").toUri();
var selector = DiscoverySelectors.selectUri(uri); Defensive patterns
Strategy: validation
Validate before calling
URI uri;
try { uri = new URI(rawString); }
catch (URISyntaxException e) { throw new IllegalArgumentException("bad URI: " + rawString, e); }
var selector = DiscoverySelectors.selectUri(uri); // URI overload avoids re-parse Type guard
static boolean isParsableUri(String s) {
try { new URI(s); return true; } catch (URISyntaxException e) { return false; }
} Try / catch
try {
return DiscoverySelectors.selectUri(rawString);
} catch (PreconditionViolationException e) {
throw new IllegalArgumentException("Not a valid URI: " + rawString, e.getCause());
} Prevention
- Pass a pre-built java.net.URI to selectUri(URI).
- For files, use selectFile(path) instead of a file:// URI.
- Percent-encode any user-derived string before treating it as a URI.
When it happens
Trigger: Calling DiscoverySelectors.selectUri(s) with an unencoded URI containing illegal characters (spaces, stray brackets, backslashes, or a missing scheme). Note: passing a java.net.URI object to selectUri(URI) bypasses this entirely.
Common situations: Passing a Windows file path (C:\Users\...) instead of a file:// URI; copy-pasting a URL with spaces or query characters not percent-encoded; mixing file: and raw paths; using a relative path where an absolute URI is expected.
Related errors
- '%s' is not a well-formed UniqueId segment
- Failed to transform configuration parameter with key '%s' an
- Could not find any resource(s) with name: ${this.classpathRe
- Failed to retrieve canonical path for file: ${file}
- Failed to retrieve canonical path for directory: ${directory
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/385dbbe6e004900d.json.
Report an issue: GitHub.