SONARJAVA-6942: Implemented rule S9389 - Data provider names should be unique within a class - #6125
Conversation
…e unique within a class This rule detects duplicate @dataProvider names within a single TestNG test class. When multiple methods are annotated with @dataProvider(name = "...") using the same name, TestNG cannot reliably determine which provider to use, leading to unpredictable test behavior. The implementation follows the pattern established by TestsStabilityCheck for annotation detection and uses a map to track first occurrences of each provider name. Files added: - DataProviderNameUniquenessCheck.java: Rule implementation - DataProviderNameUniquenessCheckTest.java: Test class - DataProviderNameUniquenessCheckSample.java: Test sample with compliant/non-compliant examples
- Fall back to method name when @dataProvider has no explicit name attribute - Report issues on method identifier instead of whole method tree - Use LiteralUtils.trimQuotes for consistent string comparison - Remove dead branches (null check, MEMBER_SELECT) to improve coverage - Add Tree.Kind.RECORD to nodesToVisit - Add test cases for implicit/explicit name collision and records - Delete scratch design note from repo root Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Make isDataProviderAnnotation and extractNameAttribute static (S2325), add test cases for non-string-literal name attributes and non-name annotation attributes to increase coverage above 90%. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use ExpressionTree.asConstant() to resolve compile-time constants in @dataProvider name attributes instead of only handling string literals. When a name attribute is present but cannot be resolved, skip the method rather than falling back to the method name. This prevents false positives and false negatives with constant references. Also makes extractDataProviderName static to fix S2325. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add ^ underlines to all Noncompliant lines in the test sample to verify exact issue positions on method identifiers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
nathsou
left a comment
There was a problem hiding this comment.
The implementation looks good overall. I left one non-blocking edge case to address.
| if (nameExpression == null) { | ||
| return method.simpleName().name(); | ||
| } | ||
| return nameExpression.asConstant(String.class).orElse(null); |
There was a problem hiding this comment.
@DataProvider(name = "") needs the same fallback as an omitted name. TestNG replaces an empty name with the method name, whereas this currently returns "". This creates both a false positive for two differently named methods that each specify name = "", and a false negative when foo() uses name = "" while another provider explicitly uses name = "foo". Could we normalize an empty resolved constant to method.simpleName().name() and cover both cases?
…S9389 TestNG treats @dataProvider(name = "") the same as omitting the name attribute, falling back to the method name. This change normalizes empty resolved names to the method name, preventing false positives when two methods specify name="" and false negatives when a method with name="" collides with an explicit name matching that method. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code Review ✅ Approved 9 resolved / 9 findingsImplements rule S9389 to detect duplicate ✅ 9 resolved✅ Edge Case: Providers relying on the default (method) name are never compared
✅ Quality: Issue is reported on the whole method instead of a narrow location
✅ Quality: Dead branches in extractNameAttribute and getAttributeName
✅ Edge Case: Records are not visited, so providers declared in a record are missed
✅ Quality: String literal values are compared with quotes and escapes unnormalized
...and 4 more resolved from earlier reviews Review coverageFunctional validation 1 of 1 objectives covered Implementation Status ✅ 1 of 1 objectives covered✅ SONARJAVA-6942 - 1 of 1 objectives coveredThis PR implements rule S9389 to ensure data provider names are unique within a class. ✅ 1 covered here
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




This PR implements rule S9389 which detects duplicate @dataProvider names within a single TestNG test class.
Summary
When multiple methods are annotated with using the same name, TestNG cannot reliably determine which provider to use, leading to unpredictable test behavior. This rule identifies such duplicates and reports them as issues.
Implementation Details
Test Coverage
Files Changed