junit-team/junit5 · error · JUnitException

Class must not be null

Error message

Class must not be null

What it means

Thrown by LogRecordListener.stream(Class) when clazz is null. Because the class cannot use Preconditions (package cycle), it checks directly and raises JUnitException 'Class must not be null' before filtering records by the class's logger name.

Solutions

  1. Pass the concrete Class object, e.g. listener.stream(MyService.class).
  2. Null-check the Class before calling stream(clazz) and fail with a clearer message if absent.
  3. Use stream() to get all records when no class filter is needed.

Example fix

// before
assertThat(listener.stream(clazz)).isNotEmpty(); // clazz may be null

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

Strategy: validation

Validate before calling

Class<?> clazz = resolveClass();
if (clazz == null) throw new IllegalArgumentException("Class required");
listener.stream(clazz);

Prevention

When it happens

Trigger: Calling listener.stream((Class<?>) null). The `if (clazz == null)` guard fires.

Common situations: Forwarding a Class reference that resolved to null (e.g. from a Map.get). Test helper passing a null class into the listener API. Generic helper losing the class token.

Related errors


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

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