False positive for @Nested tests in rule java:S2187 - sonar cloud

  • What language is this for? Java
  • Which rule? java:S2187
  • Why do you believe it’s a false-positive/false-negative? Because the class contains tests, they are just in a nested class for better code organization.
  • Are you using
    • SonarQube Cloud? Yes
  • How can we reproduce the problem?

With this class:

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

import static org.assertj.core.api.Assertions.assertThat;

class StringTest {

    @Nested
    @DisplayName("Char tests")
    class CharTest {

        @Test
        void shouldGetCharAtPosition() {
            String s = "Hello";
            
            char c = s.charAt(1);
            
            assertThat(c).isEqualTo('e');
        }
    }
}

A blocker issue is raised:

Possibly the same as described in: java:S2187 false positive for @Nested tests

This seems to be a regression, since support was added in 2018: Add support for @Nested test classes (JUnit5) by olcbean · Pull Request #1878 · SonarSource/sonar-java · GitHub

Hey there!

Are you using Automatic Analysis, or CI-based analysis?

I believe it’s CI-based analysis, since we use it integrated with azure devops. I noticed this issue raised by sonar in a pull request.

Thanks! Can you share how the analysis is configured in your Azure Pipelines YML?