False Positive on java:S1144

Must-share information (formatted with Markdown):

  • which versions are you using:
    • SonarQube Enterprise Edition v10.6 (92116)

Hello Sonar team,

Looks like we have a false positive in our tests with java:S1144 Remove this unused private when using @ParametrizedTest and @MethodSource.

When using @MethodSource without any parameter JUnit will search for a source method with the same name than the test method.

In our example, SQ raise an issue on the private static Stream<Arguments> should_throw_exception_on_unsuccessful_batch_status()

    private static Stream<Arguments> should_throw_exception_on_unsuccessful_batch_status() {
        return Stream.of(
                Arguments.of(BatchStatus.FAILED),
                Arguments.of(BatchStatus.ABANDONED),
                Arguments.of(BatchStatus.UNKNOWN)
        );
    }

    @ParameterizedTest
    @MethodSource
    void should_throw_exception_on_unsuccessful_batch_status(BatchStatus batchStatus) throws Exception {
        //GIVEN
        Job job = mock(Job.class);
        doReturn(job).when(jobStrategyResolver).getJob(ReportingType.CRS);
        JobExecution jobExecution = mock(JobExecution.class);
        doReturn(batchStatus).when(jobExecution).getStatus();
        doReturn(jobExecution).when(jobLauncher).run(eq(job), any());
        //WHEN //THEN
        assertThatExceptionOfType(BatchRuntimeException.class)
                .isThrownBy(() -> commandLineJobRunner.run());
    }

I believe this is clearly a false positive :wink:

With :heart:
Xav

Hello dear Xavier,

Does this existing ticket cover your case?

SONARJAVA-5099

With :heart:
Colin

Exactly!

You’re the boss :wink:

Great. It’s scheduled for an ongoing hardening sprint.

Happy hardening for the Java team then!

There is an other test case that is not currently covered when there are 2 or more method sources gives:

class FalsePositiveTest {

  @ParameterizedTest
  @MethodSource({"source1", "source2"})
  void test(final int index) {
    assertNotEquals(0, index);
  }

  private static int[] source1() {
    return new int[] {1, 2, 3};
  }

  private static int[] source2() {
    return new int[] {11, 12, 13};
  }
}

Both source methods will be marked with java:S1144 in SonarQube Developer Edition v10.6 (92116).