SonarQube marks function refered by MethodSource as Unused "private" method per rule 1144

  • UsingSonarQube Enterprise Edition Version 8.9.7 (build 52159)
  • I am trying to use the new MethodSource functionality from Junit 5, but SonarQube marks the referenced function as an unused private method.

As far as I can see, SonarQube should support Junit 5, so I don’t know why it flags these functions as unused.

Hi,

Welcome to the community!

I’m a bit confused by your question. Are you saying that S1144 is raising issues on test files? From what I can tell that rule is for source files only.

 
Ann

Hi,

Thank you!

Yes, the rule is raised in test files.

From what I read on the rule: [RSPEC-1144] - Jira it states in the Analysis Scope that Test Sources are part of it.

And I would very much think that it’s just as important to avoid unused code in test code as in production code.

Hi,

That Jira repo is vestigial; the rules got moved into Git a while ago. But even when it was in use, that scope field was largely aspirational. It got honored for C# but nothing else. Since then some work has been done around what rules apply to tests, but the scope of that was never clear to me.

Can you provide a reproducer snippet?

 
Ann

The function provideFToA is marked as an unused private method by SonarQube:

  @ParameterizedTest
  @MethodSource("provideFToA")
  void getAFromF_fIsDefined_aIsCorrect(String f, A eA) {
    A a = AM.getAFromF(f);

    assertThat(a).isEqualTo(eA);
  }

  private static Stream<Arguments> provideFToA() {
    return Stream.of(
      Arguments.of("ABC", A.ABC),
      Arguments.of("DEF", A.DEF),
      Arguments.of("invalid", A.INVALID),
      Arguments.of(null, A.NULL)
    );
  }

I have obfuscated the code a bit.

Some other examples can also be found here: junit5_workshop/ParameterizedTests.java at 917b4d59b1820c474a7e1ab6e18c5ee210914a15 · kousen/junit5_workshop · GitHub

1 Like

Hi,

Thanks for the code snippet.

I see work on this rule since 8.9, but nothing that seems relevant (to me) to your case, so I’m going to flag this for developer attention.

 
Ann