junit-team/junit5 · error · PreconditionViolationException
Could not find any resource(s) with name: ${this.classpathRe
Error message
Could not find any resource(s) with name: ${this.classpathResourceName} What it means
Thrown by ClasspathResourceSelector.getResources() (line 125-138) when lazy resolution via ResourceSupport.tryToGetResources succeeds but returns an empty set, i.e. no classpath/module-path entry contains a resource with the given name. It is a PreconditionViolationException, distinct from the 'Could not load' variant which fires when the lookup itself fails (line 128-130).
Source
Thrown at junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/ClasspathResourceSelector.java:132
/**
* Get the selected {@link Resource resources}.
*
* <p>If the {@link Resource resources} were not provided, but only their name,
* this method attempts to lazily load the {@link Resource resources} based on
* their name and throws a {@link PreconditionViolationException} if the
* resource cannot be loaded.
*
* @since 1.14
*/
@API(status = MAINTAINED, since = "1.14")
public Set<Resource> getResources() {
if (this.resources == null) {
Try<Set<Resource>> tryToGetResource = ResourceSupport.tryToGetResources(this.classpathResourceName);
Set<Resource> classpathResources = tryToGetResource.getNonNullOrThrow( //
cause -> new PreconditionViolationException( //
"Could not load resource(s) with name: " + this.classpathResourceName, cause));
if (classpathResources.isEmpty()) {
throw new PreconditionViolationException(
"Could not find any resource(s) with name: " + this.classpathResourceName);
}
this.resources = unmodifiableSet(classpathResources);
}
return this.resources;
}
/**
* Get the selected {@code FilePosition} within the classpath resource.
*/
public Optional<FilePosition> getPosition() {
return Optional.ofNullable(this.position);
}
/**
* @since 1.3
*/
@API(status = STABLE, since = "1.3")View on GitHub (pinned to 956246301e)
Solutions
- Confirm the resource exists: getClass().getClassLoader().getResource(name) returns non-null before building the selector.
- Drop any leading '/' (the constructor already does, so passing 'com/foo/data.json' directly is correct).
- If the resource is in a named module, ensure the containing package is open to the framework (opens/exports) so the resource is resolvable.
- Rebuild the project to ensure src/test/resources is copied to the output directory / packaged into the jar under test.
Example fix
// before
var selector = DiscoverySelectors.selectClasspathResource("com/example/data.json");
// ... resources missing on classpath -> throws on resolution
// after
String name = "com/example/data.json";
if (Thread.currentThread().getContextClassLoader().getResource(name) == null) {
throw new IllegalStateException("resource not on classpath: " + name);
}
var selector = DiscoverySelectors.selectClasspathResource(name); Defensive patterns
Strategy: validation
Validate before calling
String name = "com/example/data.json";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
boolean exists = cl.getResource(name) != null;
if (!exists) throw new IllegalStateException("resource not on classpath: " + name);
var selector = DiscoverySelectors.selectClasspathResource(name); Try / catch
try {
selector.getResources(); // forces lazy resolution
} catch (PreconditionViolationException e) {
throw new IllegalStateException("classloaders missing the resource", e);
} Prevention
- Confirm getClass().getClassLoader().getResource(name) is non-null before building the selector.
- For named modules, ensure the owning package is open to the framework.
- Do not prefix the name with a leading slash (the constructor strips one anyway).
When it happens
Trigger: Calling DiscoverySelectors.selectClasspathResource(name) and then getResources()/getClasspathResources() (or letting an engine resolve it) when 'name' does not match any resource resolvable by the context ClassLoader. The constructor strips a leading '/' so a name like '/foo.json' becomes 'foo.json'; mismatches in package-style paths matter.
Common situations: Resource lives in src/test/resources but the test runs with a classpath that omits it (shading/relocating); wrong path separator or case sensitivity on case-sensitive filesystems; resource packaged in a jar that isn't on the classpath; module-path resource not opened/exports correctly; typo in the resource name.
Related errors
- OutputDirectoryCreator not available; probably due to unalig
- '%s' is not a well-formed UniqueId segment
- Failed to create a java.net.URI from: ${uri}
- 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/29d7c44f8bf65efe.json.
Report an issue: GitHub.