Please provide
- Operating system: Mac OSX
- IDE name and flavor/env: IntelliJ
- SonarLint plugin version: 6.7.0.45926
- Is connected mode used: Yes
- Connected to SonarCloud or SonarQube (and which version): SonarQube
And a thorough description of the problem / question:
Sonar is incorrectly identifying a test module as being production code. As such it complains about
Assertions should not be used in production code
Bug
Major
java:S5960
Assertions are intended to be used in test code, but not in production code. It is confusing, and might lead to ClassNotFoundException when the build tools only provide the required dependency in test scope.
In addition, assertions will throw a sub-class of Error: AssertionError, which should be avoided in production code.
This rule raises an issue when any assertion intended to be used in test is used in production code.
Supported frameworks:
- JUnit
- FestAssert
- AssertJ
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).
If I rename my package to include test
or if I switch over to a SpringFramework assertion the issue goes away. But the test in question has a @Test
annotation. It seems artificial to rename or switch assertions purely due to a Sonar limitation.
Is there another workaround?