Rule S5960: Assertions should not be used in production code does not consider "it" packages

  • versions used: SonarQube 8.9.1
  • minimal code sample to reproduce
    Add a simple Java Class that contains a test method and an assert in a package that does not contain the words test, junit or assert.

Any class that contains test methods and asserts that would be included in a source package that does not include the words “test”, “junit” or “assert” will trigger this rule.
But the rule does not consider asserts found in packages that include the work “it”, which is a possibility as per the maven standard directory layout. Would it be possible to add the “it” word to the list of exceptions also, since junit and assert ar also included, not just test?

Hi @madalinaptj,

Maven standard directory src/it is a directory and not a package, and the rule S5960 should currently not report issues on java files in src/it.
The rule S5960 only target java files in src/main/java, and in the rule implementation there’s an exception when the analyzed package contains test|junit|assert (in AssertionsInProductionCodeCheck.java:L41).
It’s not so easy to include it in this exclusion list because it would exclude for example “com.twitter” or Italian domains like it.ozimov.
But we could probably exclude when one part of the package name exactly equals to it, ignoring the first part. I have created this ticket SONARJAVA-4022.

Thanks for your feedback,

Alban

1 Like

You are right, I meant the src/it directory. I apologize, I wrote the post in a hurry and did not pay enough attention.
If the rule is meant to target only the src/main/java directory, then I’m not sure I understand why the rule is triggered for classes in our src/it directory. We also include the word it in the package hierarchy, so either way would help us to avoid adding an exception for this rule. Thank you for reviewing this!

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