Skip to content

SONARJAVA-5708 Implement rule S8924: Mockito core methods should be imported statically#5689

Open
NoemieBenard wants to merge 10 commits into
masterfrom
nb/implement-S8924
Open

SONARJAVA-5708 Implement rule S8924: Mockito core methods should be imported statically#5689
NoemieBenard wants to merge 10 commits into
masterfrom
nb/implement-S8924

Conversation

@NoemieBenard

@NoemieBenard NoemieBenard commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • New rule implementation:
    • Implemented MockitoStaticImportCheck (rule S8924) to enforce static imports for common Mockito methods.
  • Check coverage:
    • Detects non-compliant usage of methods like mock, when, verify, spy, any, and eq when called via Mockito. qualifier.
  • Testing:
    • Added MockitoStaticImportCheckTest and corresponding sample file MockitoStaticImportCheckSample.java to verify rule reporting.

This will update automatically on new commits.

@NoemieBenard NoemieBenard marked this pull request as draft June 23, 2026 08:26
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-5708

@NoemieBenard NoemieBenard marked this pull request as ready for review June 23, 2026 09:31
@NoemieBenard NoemieBenard requested a review from rombirli June 23, 2026 09:45
@gitar-bot

gitar-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 resolved / 2 findings

Implements rule S8924 to enforce static imports for common Mockito methods, including all necessary metadata files. No remaining issues found.

✅ 2 resolved
Bug: Missing rule metadata files S8924.json / S8924.html

📄 java-checks/src/main/java/org/sonar/java/checks/tests/MockitoStaticImportCheck.java:29
The new rule MockitoStaticImportCheck is annotated @Rule(key = "S8924"), but no metadata resources exist for it. Every other rule has a <key>.json and <key>.html under sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/ (e.g. S6068.json/S6068.html), and JavaRulesDefinition loads them via RuleMetadataLoader.addRulesByAnnotatedClass. A Glob for **/rules/java/S8924.* returns nothing.

Without these files the rule will not be registered with a name, description, type, severity, SQALE/remediation, or be includable in the Sonar_way profile, and JavaRulesDefinition/rule-metadata tests will fail to load the rule. Add S8924.json (with the appropriate Tests scope so it is picked up for the quality profile) and S8924.html description. This is expected for a draft PR, but is required before the rule can ship.

Quality: S8924 declares "quickfix":"targeted" but no quick fix is implemented

📄 sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8924.json:18 📄 java-checks/src/main/java/org/sonar/java/checks/tests/MockitoStaticImportCheck.java:44-56
The rule metadata sets "quickfix": "targeted", which advertises to clients (e.g. SonarLint/IDEs) that an automatic fix is available. However, MockitoStaticImportCheck.visitNode only calls reportIssue(methodSelect, ...) and never registers a JavaQuickFix. Other rules using "targeted"/"covered" (e.g. UnusedLocalVariableCheck) attach a quick fix via reportIssue(...) with a list of JavaQuickFix objects. There is no consistency test, so this will not fail the build, but the rule promises a fix that is never delivered.

This rule is an ideal quick-fix candidate: the fix is mechanical (remove the Mockito. prefix and add the corresponding import static org.mockito.Mockito.<method>;). Either implement the quick fix and verify it with [[quickfixes=...]] annotations / QuickFixVerifier, or change the metadata to "quickfix": "infeasible" (or "partial") to match the current implementation.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant