Skip to content

Null loggers when running unit tests in parallel with Junit5 #4318

@bfreitastgtg

Description

@bfreitastgtg

Steps to reproduce

Despite trying, I unfortunately was not able to come up with a way to reproduce this outside of our project.

Context

  • Used versions (Jupiter/Vintage/Platform): org.junit.jupiter:junit-jupiter:5.11.4
  • Build Tool/IDE: Gradle 8.12 + IntelliJ
  • Java 21.0.3

Hello,
We have a project with thousands of unit tests. We are looking into speeding up their execution by enabling parallelization of the unit tests with this feature from JUnit5:

systemProperties["junit.jupiter.execution.parallel.enabled"] = true
systemProperties["junit.jupiter.execution.parallel.mode.default"] = "same_thread"
systemProperties["junit.jupiter.execution.parallel.mode.classes.default"] = "concurrent"
systemProperties["junit.jupiter.execution.parallel.config.strategy"] = "fixed"
systemProperties["junit.jupiter.execution.parallel.config.fixed.parallelism"] = 6
systemProperties["junit.jupiter.execution.parallel.config.fixed.max-pool-size"] = 6

However, a lot of tests become flaky when we do that. And we noticed it's because any class that uses a org.slf4j.Logger instance may end with their logger set to null:

package com.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foobar {
  private static Logger log = LoggerFactory.getLogger(Foobar.class);

  public void someMethod() {
    logger.warn("Something");
  }
}
Cannot invoke "org.slf4j.Logger.warn(String)" because "com.example.Foobar.log" is null
java.lang.NullPointerException: Cannot invoke "org.slf4j.Logger.warn(String)" because "com.example.Foobar.log" is null

The issue happens even when using a trivial logging implementation, slf4j-nop. The tests run fine when not parallelized.

Linking this to qos-ch/slf4j#449

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions