junit-team/junit5 · error · JUnitException

Class must not be null

Error message

Class must not be null

What it means

LoggerFactory.getLogger(Class<?>) throws JUnitException when clazz is null; it uses clazz.getName() as the logger name. The factory is @INTERNAL and not part of the public API.

Source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LoggerFactory.java:50

	private LoggerFactory() {
		/* no-op */
	}

	private static final Set<LogRecordListener> listeners = ConcurrentHashMap.newKeySet();

	/**
	 * Get a {@link Logger} for the specified class.
	 *
	 * @param clazz the class for which to get the logger; never {@code null}
	 * @return the logger
	 */
	@SuppressWarnings("ConstantValue")
	public static Logger getLogger(Class<?> clazz) {
		// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here
		// since that would introduce a package cycle.
		if (clazz == null) {
			throw new JUnitException("Class must not be null");
		}

		return new DelegatingLogger(clazz.getName());
	}

	/**
	 * Add the supplied {@link LogRecordListener} to the set of registered
	 * listeners.
	 */
	public static void addListener(LogRecordListener listener) {
		listeners.add(listener);
	}

	/**
	 * Remove the supplied {@link LogRecordListener} from the set of registered
	 * listeners.
	 */
	public static void removeListener(LogRecordListener listener) {

View on GitHub (pinned to 956246301e)

Solutions

  1. Pass a concrete Class to getLogger.
  2. Null-check the class before calling.
  3. Prefer java.util.logging.Logger.getLogger(name) directly in non-JUnit code.

Example fix

// before
LoggerFactory.getLogger(maybeNull);

// after
LoggerFactory.getLogger(MyService.class);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> clazz = null;
if (clazz == null) {
    throw new IllegalArgumentException("Class must not be null");
}
LoggerFactory.getLogger(clazz);

Type guard

static <T> Class<T> requireClass(Class<T> c) {
    return java.util.Objects.requireNonNull(c, "Class must not be null");
}

Try / catch

try {
    LoggerFactory.getLogger(clazz);
} catch (JUnitException e) {
    if (e.getMessage().equals("Class must not be null")) {
        // pass a concrete Class
    } else throw e;
}

Prevention

When it happens

Trigger: Calling LoggerFactory.getLogger(null), typically from internal JUnit code or a plugin reusing the internal LoggerFactory.

Common situations: Internal code path that resolved a class to null; third-party integration using the internal logging API with a null class.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/392d375ab91c3994.json. Report an issue: GitHub.