junit-team/junit5 · error · JUnitException

Failed to load class with name

Error message

Failed to load class with name '${binaryName}'.

What it means

JUnitException thrown by ModuleReferenceClassScanner.loadClassUnchecked when ClassLoader.loadClass(binaryName) throws ClassNotFoundException. The class file was visible to the module scanner but the runtime ClassLoader cannot resolve it to a Class object.

Solutions

  1. Ensure the module exports/contains the package to the test layer
  2. Remove orphan or stale .class files from the module
  3. Rebuild after relocation/shading so paths match binary names
  4. Align the scanning ClassLoader with the runtime ClassLoader
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Module scanning finds a class file (e.g. com/foo/Bar.class) and derives binary name com.foo.Bar, but classLoader.loadClass("com.foo.Bar") fails - classloader visibility gap, missing dependency, or binary-name/path mismatch (nested classes, shading, module encapsulation).

Common situations: JPMS module does not export or contain the package to the test classloader; shaded or relocated classes whose file path no longer matches their binary name; orphan .class files left in a module; split packages across modules.

Related errors


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

Appendix: source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/util/ModuleUtils.java:283

					// @formatter:on
				}
			}
			catch (IOException e) {
				throw new JUnitException("Failed to read contents of " + reference + ".", e);
			}
		}

		/**
		 * Load class by its binary name.
		 *
		 * @see ClassLoader#loadClass(String)
		 */
		private Class<?> loadClassUnchecked(String binaryName) {
			try {
				return classLoader.loadClass(binaryName);
			}
			catch (ClassNotFoundException e) {
				throw new JUnitException("Failed to load class with name '" + binaryName + "'.", e);
			}
		}

	}

	/**
	 * {@link ModuleReference} resource class scanner.
	 *
	 * @since 1.11
	 */
	static class ModuleReferenceResourceScanner {

		private final ResourceFilter resourceFilter;
		private final ClassLoader classLoader;

		ModuleReferenceResourceScanner(ResourceFilter resourceFilter, ClassLoader classLoader) {
			this.resourceFilter = resourceFilter;
			this.classLoader = classLoader;

View on GitHub (pinned to f070c699a0)