java:S2187 false positive for @Nested tests

Hello everyone,

I’ve noticed that rule java:S2187 adds an issue “Add some tests to this class.” even if test is structured as:

class SomeTest {
  @Nested
  class Nested {
    @Test
    void testSomething() {
       ...
    }
  }
}

I am using SonarQube Community Build v24.12.0.100206.

Thank you for great product you’ve built!

Hi,

Welcome to the community!

Only the latest version of SonarQube Community Build is considered active, so you’ll need to update and see if the situation is still replicable before we can help you.

Your update path is:

24.12 → 25.8

You may find these resources helpful:

If you have questions about upgrading, feel free to open a new thread for that here.

If your FP persists after update, please come back to us!

Hi!

I’m using SonarQube Cloud and this same false-positive situation happens here.

Hi @mfolnovic,
Sorry for the late reply. Are you still experiencing this issue?

Unfortunately, I am unable to reproduce the issue. Could you help us pin down the issue with some more details?

I have tried with the following sample.

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class NestedClassIsOK {
  @Nested
  class NestedTestClass {
    @Test
    void testSomething() {
      // Nothing to worry about
    }
  }
}
  1. You mention running having the issue both with SonarQube Community Build and SonarQube Cloud. So I am assuming your analysis runs in CI and not with Automtatic analysis. Is that correct?
  2. Are the annotations coming for JUnit? If so which version?

Your answer would really help us address the issue here. So thanks in advance.

Cheers,

Dorian

It’s been a while, so I’m either misremembering or something was fixed in the meantime. :slight_smile:

The only scenario I see this still happening is if I only have @ParameterizedTest, so no @Test annotations. I am using JUnit 5.12.2. Snippet:

import java.util.stream.Stream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class NestedClassTest {
  @Nested
  class NestedTestClass {
    @ParameterizedTest
    @MethodSource("parameters")
    void testSomething(int a, int b) {
      Assertions.assertEquals(a, b);
    }

    static Stream<Arguments> parameters() {
      return Stream.of(
          Arguments.of(1, 1)
      );
    }
  }
}

I am not using SonarQube Cloud, just SonarQube Community Build, so analysis is running through CI directly with sonar-scanner (7.3.0.5189).

Hey @mfolnovic,

Thanks for sharing this new snippet. Unfortunately, I am still unable to reproduce the issue with the new sample.

You mention using the sonar-scanner. Did you manually configure the list of test libraries, under the property sonar.java.test.libraries, to point to the right version of JUnit?

Cheers,

Dorian

Hi!

I’m seeing this too on SonarCloud.

Here’s an example: SonarQube Cloud

Hey there, just to make sure I investigate in the correct direction, are you also user the sonar-scanner @rahulsom ? Or are you relying on the maven/gradle scanner?
As the issue might be related to broken semantics