junit-team/junit5 · error · JUnitException

Class must not be null

Error message

Class must not be null

What it means

LogRecordListener.stream(Class<?>) throws JUnitException when clazz is null, because it calls clazz.getName() to match logger names. This is an @INTERNAL API used by JUnit's own tests.

Source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LogRecordListener.java:109

	 * {@linkplain #logRecordSubmitted submitted} to this listener by the current
	 * thread for the logger name equal to the name of the given class.
	 *
	 * <p>As stated in the Javadoc for {@code LogRecord}, a submitted
	 * {@code LogRecord} should not be updated by the client application. Thus,
	 * the {@code LogRecords} in the returned stream should only be inspected for
	 * testing purposes and not modified in any way.
	 *
	 * @param clazz the class for which to get the log records; never {@code null}
	 * @see #stream()
	 * @see #stream(Level)
	 * @see #stream(Class, Level)
	 */
	@SuppressWarnings("ConstantValue")
	public Stream<LogRecord> stream(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 stream().filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()));
	}

	/**
	 * Get a stream of {@link LogRecord log records} that have been
	 * {@linkplain #logRecordSubmitted submitted} to this listener by the current
	 * thread for the logger name equal to the name of the given class at the given
	 * log level.
	 *
	 * <p>As stated in the Javadoc for {@code LogRecord}, a submitted
	 * {@code LogRecord} should not be updated by the client application. Thus,
	 * the {@code LogRecords} in the returned stream should only be inspected for
	 * testing purposes and not modified in any way.
	 *
	 * @param clazz the class for which to get the log records; never {@code null}
	 * @param level the log level for which to get the log records; never {@code null}

View on GitHub (pinned to 956246301e)

Solutions

  1. Pass the concrete class whose logger you want to inspect.
  2. Use stream() and filter records manually if the class is unknown.
  3. Add a nullness check in test utilities that wrap the listener.

Example fix

// before
listener.stream(clazz);   // clazz == null

// after
listener.stream(MyService.class);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> clazz = null; // some computed value
if (clazz == null) {
    throw new IllegalArgumentException("Class must not be null");
}
listener.stream(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 {
    listener.stream(clazz);
} catch (JUnitException e) {
    if (e.getMessage().equals("Class must not be null")) {
        // pass a concrete Class instead
    } else throw e;
}

Prevention

When it happens

Trigger: Calling listener.stream((Class<?>) null).

Common situations: Test refactor that passes a null Class reference; parameterization that lost the type token.

Related errors


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