[java] UseObjectForClearerAPI conflicts with TestNG DataProvider
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 5.5k
- Forks
- 1.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 54
Description
Affects PMD Version: 6.11.0
Rule: UseObjectForClearerAPI
Description:
The UseObjectForClearerAPI rule doesn't work well with parameterised unit test APIs such as TestNG's DataProvider. With DataProvider, it's perfectly normal and usable to pass a bunch of strings (or other objects) to a method.
Code Sample demonstrating the issue:
@DataProvider(name = "dataProvider")
public Object[][] subjectLines() {
return new Object[][] {
// title, givenName, surname, organisation, receiptNumber, date, expected
{ "Mr", "Harry", "Walsh", "ABC Corporation", "1650028", new LocalDate(2009, 5, 27), "Right to Information Application - Mr Harry Walsh ABC Corporation (Receipt No: 1650028) - 27-05-2009" },
{ null, "Harry", "Walsh", null, "1650027", new LocalDate(2009, 5, 26), "Right to Information Application - Harry Walsh (Receipt No: 1650027) - 26-05-2009" },
{ null, null, null, "ABC Corporation", "1650026", new LocalDate(2009, 4, 1), "Right to Information Application - ABC Corporation (Receipt No: 1650026) - 01-04-2009" },
{ null, null, null, null, "1650025", new LocalDate(2008, 1, 27), "Right to Information Application - (Receipt No: 1650025) - 27-01-2008" },
};
}
@Test(dataProvider = "dataProvider")
public void shouldBuildSubjectLineWithDetailsSupplied(String title, String givenName, String surname, String organisation, String receiptNumber, LocalDate date, String expected) {
when(orderDetails.getNameTitle()).thenReturn(title);
when(orderDetails.getGivenName()).thenReturn(givenName);
when(orderDetails.getFamilyName()).thenReturn(surname);
when(orderDetails.getOrganisationName()).thenReturn(organisation);
when(orderStatus.getReceiptNumber()).thenReturn(receiptNumber);
String actual = builder.buildSubjectLine(orderDetails, orderStatus);
assertThat(actual).isEqualTo(expected);
}
Running PMD through: Maven
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the UseObjectForClearerAPI rule and reproduce the reported case through Maven using the TestNG DataProvider example. Done means parameterized test methods like this are no longer reported incorrectly while the rule still identifies its intended violations; the payload does not name a source file or test file.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100