Molzen
(Molzen)
January 19, 2023, 7:42am
#1
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.
ganncamp
(G Ann Campbell)
January 23, 2023, 6:46pm
#2
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
Molzen
(Molzen)
January 25, 2023, 11:18am
#3
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.
ganncamp
(G Ann Campbell)
January 25, 2023, 3:00pm
#4
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
Molzen
(Molzen)
January 26, 2023, 9:38am
#5
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
ganncamp
(G Ann Campbell)
January 26, 2023, 2:18pm
#6
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