{"record":{"id":"4da2e860a96acd4d","repo":"junit-team/junit5","slug":"class-must-not-be-null","errorCode":null,"errorMessage":"Class must not be null","messagePattern":"Class must not be null","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LogRecordListener.java","lineNumber":109,"sourceCode":"\t * {@linkplain #logRecordSubmitted submitted} to this listener by the current\n\t * thread for the logger name equal to the name of the given class.\n\t *\n\t * <p>As stated in the Javadoc for {@code LogRecord}, a submitted\n\t * {@code LogRecord} should not be updated by the client application. Thus,\n\t * the {@code LogRecords} in the returned stream should only be inspected for\n\t * testing purposes and not modified in any way.\n\t *\n\t * @param clazz the class for which to get the log records; never {@code null}\n\t * @see #stream()\n\t * @see #stream(Level)\n\t * @see #stream(Class, Level)\n\t */\n\t@SuppressWarnings(\"ConstantValue\")\n\tpublic Stream<LogRecord> stream(Class<?> clazz) {\n\t\t// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here\n\t\t// since that would introduce a package cycle.\n\t\tif (clazz == null) {\n\t\t\tthrow new JUnitException(\"Class must not be null\");\n\t\t}\n\n\t\treturn stream().filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()));\n\t}\n\n\t/**\n\t * Get a stream of {@link LogRecord log records} that have been\n\t * {@linkplain #logRecordSubmitted submitted} to this listener by the current\n\t * thread for the logger name equal to the name of the given class at the given\n\t * log level.\n\t *\n\t * <p>As stated in the Javadoc for {@code LogRecord}, a submitted\n\t * {@code LogRecord} should not be updated by the client application. Thus,\n\t * the {@code LogRecords} in the returned stream should only be inspected for\n\t * testing purposes and not modified in any way.\n\t *\n\t * @param clazz the class for which to get the log records; never {@code null}\n\t * @param level the log level for which to get the log records; never {@code null}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LogRecordListener.java#L91-L127","documentation":"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.","triggerScenarios":"Calling listener.stream((Class<?>) null). The `if (clazz == null)` guard fires.","commonSituations":"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.","solutions":["Pass the concrete Class object, e.g. listener.stream(MyService.class).","Null-check the Class before calling stream(clazz) and fail with a clearer message if absent.","Use stream() to get all records when no class filter is needed."],"exampleFix":"// before\nassertThat(listener.stream(clazz)).isNotEmpty(); // clazz may be null\n\n// after\nassertThat(listener.stream(MyService.class)).isNotEmpty();","handlingStrategy":"validation","validationCode":"Class<?> clazz = resolveClass();\nif (clazz == null) throw new IllegalArgumentException(\"Class required\");\nlistener.stream(clazz);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass the concrete Class token (e.g. MyService.class) to stream(Class).","Use stream() when class filtering is not needed.","Avoid forwarding possibly-null Class references."],"tags":["junit-platform","logging","null-check","test-listener"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}