Java Test Code: 40 more rules enabled by default

Hello Java devs,

We want to help you write clean test code!

However previously, Sonar was only using a limited set of rules on your test code, see the list here.

Sonar relies on your configuration to determine which files are the main code and which are tests. Our Maven and Gradle scanners handle this, but it can be configured, see the docs.

To improve, we’re reviewing our existing rules for main code to select those that make sense for test code as well.

As a first step, we’ve enabled two batches of 20 rules each. Thus, 40 additional rules are now enabled by default in test code!

Below is the complete list of rules. We are continuing this effort, so expect more rules in the future.

Enjoy the updates and don’t forget to let us know what you think.

1 Like

First batch:

  • S1130 - Exceptions in “throws” clauses should not be superfluous
  • S1874 - “@Deprecated” code should not be used
  • S6126 - String multiline concatenation should be replaced with Text Blocks
  • S6208 - Comma-separated labels should be used in Switch with colon case
  • S1117 - Local variables should not shadow class fields
  • S1134 - Track uses of “FIXME” tags
  • S125 - Sections of code should not be commented out
  • S5738 - “@Deprecated” code marked for removal should never be used
  • S1128 - Unnecessary imports should be removed
  • S1135 - Track uses of “TODO” tags
  • S1144 - Unused “private” methods should be removed
  • S6213 - Restricted Identifiers should not be used as Identifiers
  • S3457 - Format strings should be used correctly
  • S1481 - Unused local variables should be removed
  • S1854 - Unused assignments should be removed
  • S1172 - Unused method parameters should be removed
  • S4738 - Java features should be preferred to Guava
  • S4144 - Methods should not have identical implementations
  • S1066 - Mergeable “if” statements should be combined

Second batch:

  • S117 - Local variable and method parameter names should comply with a naming convention
  • S115 - Constant names should comply with a naming convention
  • S116 - Field names should comply with a naming convention
  • S3008 - Static non-final field names should comply with a naming convention
  • S119 - Type parameter names should comply with a naming convention
  • S114 - Interface names should comply with a naming convention
  • S1223 - Non-constructor methods should not have the same name as the enclosing class
  • S1186 - Methods should not be empty
  • S108 - Nested blocks of code should not be left empty
  • S1116 - Empty statements should be removed
  • S128 - Switch cases should end with an unconditional “break” statement
  • S1161 - “@Override” should be used on overriding and implementing methods
  • S1220 - The default unnamed package should not be used
  • S1598 - Package declaration should match source file directory
  • S1197 - Array designators “[” and “]” should be on the type, not the variable
  • S1068 - Unused “private” fields should be removed
  • S1124 - Modifiers should be declared in the correct order
  • S131 - “switch” statements should have “default” clauses
  • S1110 - Redundant pairs of parentheses should be removed
  • S1488 - Local variables should not be declared and then immediately returned or thrown

Thanks! It’s a long-awaited improvement!

1 Like

Thank you for the kind feedback @lrozenblyum!

1 Like