[java:s5960] FP! Test class not recognized

Sonar Qube: Community Build v25.7.0.110598
Lint: 11.20.0.84274
Java: 25
Eclipse: 2025-09

The Java rule s5960 raises an issue, if assertEquals() and that like are called, beause the code is not recognized as test code.

As per the rule description code is recognized as test, if the package contains the “test” token. In most cases this can be done, but sometimes not.

Code should also be identified as test case, if the Class is called something like “FooBarTest”.

Especially a method should be treated as a test, if it is annotated with one of the two @Test annotations from JUnit, which is the most certain hint.

Hi,

I’m not seeing that in the rule description. In fact, analysis recognizes tests because they’re identified as such either manually via sonar.tests or automatically by the SonarScanner for Maven or SonarScanner for Gradle.

How are your sonar.sources and sonar.tests identified for analysis?

 
Ann

I need to wait for my colleague, who manages our Sonar server. But, what I can tell, is, that the last paragraph from the rule’s description days this:

Note: this does not apply for assert from Java itself or if the source code package name is related to tests (contains: test or assert or junit).

I’m not sure, if test classes are identified as such other than that here.

Hi,

Wow. That’s what I get for skimming. :flushed_face:

I think it’s probably the description rather than the detection that’s wrong here. But I’ll flag this for the language experts and let them decide.

Sorry about that.

 
:sweat_smile:
Ann

Is it possible, that there’s a difference for Qube and Lint? I’m pretty sure, the description is correct for Lint.

I had the chance to ask our Sonar server guy. And the decision, whether to handle a test class as test is indeed done by scanner settings.

In Lint it is done as the description says.

So my question now is: Can this be changed, so that Lint recognizes tests as tests, if annotated with one of the @Test annotations. I’m not sure, if a “Test” in the class name wuold be a good idea for detection. This could also refer to some other test meaning.

1 Like

Hello, thank you for the report.
I agree that the rule could be slightly more sophisticated, and allow assertions inside annotated test methods.
I created a ticket for this.

1 Like

Fear @mfroehlich,

Is the issue shown incorrectly in Eclipse?

The implementation in SonarQube for IDE (SonarLint) relies on the Information provided by Eclipse JDT whether or not a Source Directory (and therefore the Packages and classes) are marked as Test Code or not.

Would you be so Kind to provide the .classpath file? The IDE Plug-in May raise the issues incorrectly whenever the entry for the Test Code Directory does Not Contain the Test Attribute.

This might therefore be here Both an issue with the Rule and the IDE Plug-in Not getting the correct values for the actual analysis. This behaviour differs from the analysis against SonarQube Server or Cloud, as the IDE Plug-in creates sonar.tests etc. Automatically based on the IDE and project configuration.

Cheers :clinking_glasses:

Tbh, I didn’t even know, you could flag a source folder as containing tests. I did that now.

Unfortunately the rule still hits.

So, it’s still a good idea to improve the rule to honor @Test annotations. But it should also respect that test folder setting.

Thanks for the learning. :wink:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.