junit-team/junit5 · error · PreconditionViolationException
Failed to create a java.net.URI from
Error message
Failed to create a java.net.URI from: ${uri} What it means
DiscoverySelectors.selectUri(String) constructs a java.net.URI from a string; if the string violates RFC 3986 (URISyntaxException), it is wrapped as PreconditionViolationException. The string must already be non-blank (checked just before). This guards selector construction before any discovery runs.
Solutions
- Use java.net.URI or URI.Builder/Path.toUri() to construct the string rather than concatenation, so encoding is handled.
- Percent-encode spaces and reserved characters; ensure a scheme is present (e.g. 'file://', 'http://').
- Prefer selectUri(URI) overload (passing a pre-built URI) to surface syntax errors at construction time near the source.
- Inspect URISyntaxException position from the cause to locate the bad character.
Example fix
// before
UriSelector s = selectUri("my file path with spaces.xml");
// after
UriSelector s = selectUri(new File("my file path with spaces.xml").toURI().toString());
// or
UriSelector s = selectUri(new URI("file", null, "/my%20file%20path.xml", null)); Defensive patterns
Strategy: validation
Validate before calling
// Validate URI syntax before constructing the selector.
String uriStr = userInput;
try { new java.net.URI(uriStr); } catch (java.net.URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI for selectUri: " + e.getMessage(), e);
}
// or use a real URI directly
Try / catch
try { return selectUri(new URI(raw)); } catch (URISyntaxException e) { /* convert/escape or ask caller to fix */ } Prevention
- Construct URIs with java.net.URI or Path.toUri() rather than string concatenation.
- Prefer the selectUri(URI) overload so syntax errors surface at construction time near the source.
- Percent-encode spaces and reserved characters; always include a scheme.
When it happens
Trigger: Calling selectUri(string) with a syntactically invalid URI: missing scheme, illegal characters (spaces, unencoded brackets), a path needing percent-encoding, or a malformed authority. URISyntaxException's reason points at the offending position.
Common situations: Passing a file path with spaces as a URI without percent-encoding; missing scheme ('host/path' instead of 'file://...'); copy-pasted URIs with stray characters; building URIs by string concatenation without URI-building utilities.
Related errors
- Could not find any resource(s) with name
- Failed to retrieve canonical path for directory
- Failed to retrieve canonical path for file
- OutputDirectoryCreator not available; probably due to…
- ' ' is not a well-formed UniqueId segment
AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11).
Data as JSON: /api/errors/385dbbe6e004900d.
Report an issue: GitHub.
Appendix: 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 f070c699a0)