junit-team/junit5 · error · JUnitException

Level must not be null

Error message

Level must not be null

What it means

LogRecordListener.stream(Level) throws JUnitException when level is null. The API is @INTERNAL (for JUnit's own test suite); it filters captured log records by reference equality on the Level, so a non-null level is required.

Source

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

	 * thread 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 level the log level for which to get the log records; never {@code null}
	 * @since 1.4
	 * @see #stream()
	 * @see #stream(Class)
	 * @see #stream(Class, Level)
	 */
	@SuppressWarnings({ "ConstantValue", "ReferenceEquality" })
	public Stream<LogRecord> stream(Level level) {
		// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here
		// since that would introduce a package cycle.
		if (level == null) {
			throw new JUnitException("Level must not be null");
		}

		return stream().filter(logRecord -> logRecord.getLevel() == level);
	}

	/**
	 * 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.
	 *
	 * <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)

View on GitHub (pinned to 956246301e)

Solutions

  1. Pass a concrete java.util.logging.Level (e.g. Level.WARNING).
  2. Use stream() without a level argument and filter the records manually.
  3. Run a nullness checker (Checker Framework / IDE inspection) on test code using the listener.

Example fix

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

// after
listener.stream(Level.WARNING);
Defensive patterns

Strategy: validation

Validate before calling

java.util.logging.Level level = null; // some computed value
if (level == null) {
    throw new IllegalArgumentException("Log level must not be null");
}
listener.stream(level);

Type guard

static java.util.logging.Level requireLevel(java.util.logging.Level l) {
    return java.util.Objects.requireNonNull(l, "Level must not be null");
}

Try / catch

try {
    listener.stream(level);
} catch (JUnitException e) {
    if (e.getMessage().equals("Level must not be null")) {
        // pass a concrete Level instead
    } else throw e;
}

Prevention

When it happens

Trigger: Calling listener.stream((Level) null) in a test that asserts on captured JUL records.

Common situations: A Level field that resolved to null; refactor that left a null literal; passing a computed Level that returned null.

Related errors


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