Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bfreitastgtg opened this issue Feb 13, 2025 · 1 comment
Closed

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

bfreitastgtg opened this issue Feb 13, 2025 · 1 comment

Comments

@bfreitastgtg
Copy link

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

@marcphilipp
Copy link
Member

According to qos-ch/slf4j#449 (comment), the suspected root cause is in commons-logging. We're not doing anything in JUnit that would mess with class initialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants