junit-team/junit5 · error · PreconditionViolationException

Could not find any resource(s) with name

Error message

Could not find any resource(s) with name: ${this.classpathResourceName}

What it means

ClasspathResourceSelector.getResources() first tries to load Resource(s) for the configured name (failure yields 'Could not load', a different message), then, if the resulting set is empty, throws PreconditionViolationException 'Could not find any resource(s)'. So this specific message means the lookup succeeded but matched zero resources on the classpath.

Solutions

  1. Confirm the resource name exactly matches a path on the classpath (use '/' separators, no leading slash): 'META-INF/foo.json'.
  2. Ensure the containing jar/directory is actually on the classpath/module path passed to the launcher.
  3. If the resource is generated, build the project first so it lands on the classpath before discovery runs.
  4. Distinguish this 'not found' (empty result) from 'could not load' (I/O error) — fix the name first, then the classpath.

Example fix

// before
LauncherDiscoveryRequest r = request().select(selectClasspathResource("meta-inf/foo.json")).build();
// after — exact path with correct case and separators
LauncherDiscoveryRequest r = request().select(selectClasspathResource("META-INF/foo.json")).build();
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the resource exists on the classpath before discovery.
String name = "META-INF/foo.json"; // '/'-separated, no leading slash, exact case
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null || Collections.list(cl.getResources(name)).isEmpty())
  throw new IllegalArgumentException("No classpath resource named '" + name + "' visible to the launcher classloader");

Prevention

When it happens

Trigger: Selecting a classpath resource by name (selectClasspathResource) where nothing matches: a typo, wrong path separator, the resource is on a classpath root that is not actually present, or the name refers to a package/directory rather than a resource file.

Common situations: Discovery using --select-resource with a misspelled name; the resource lives in a test source dir not copied to the classpath; a Gradle/Maven setup that filters resources and the name does not match post-filtering; slash vs dot confusion.

Related errors


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

Appendix: 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 f070c699a0)