Rule S9024 has been activated a few days ago in the built-in sonar way profile on our sonarqube cloud US server.
I believe this rule is detrimental, and should either be removed or at the very least be deactivated by default (and users can enable it should they wish to do so).
Using Mockito’s @InjectMocks is widely considered a bad practice because it fails silently when it cannot inject dependencies, relies on fragile reflection tricks, and masks poor architectural design.
When using constructor injection (which is a best practice), if the constructor is modified, you will get no compilation error in the test (which helps understand which tests should be adjusted following the modification.)
It “encourages” developers/agents to use field injection - which I believe Sonar itself flags as an issue.
Environment:
- ALM: Github
- CI: GH Actions
- Languages of the repository: java
- SonarQube Cloud US