Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UseObjectForClearerAPI conflicts with TestNG DataProvider #1867

Open
ThrawnCA opened this issue Jun 13, 2019 · 1 comment
Open

[java] UseObjectForClearerAPI conflicts with TestNG DataProvider #1867

ThrawnCA opened this issue Jun 13, 2019 · 1 comment
Labels
an:enhancement An improvement on existing features / rules

Comments

@ThrawnCA
Copy link

ThrawnCA commented Jun 13, 2019

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

@adangel
Copy link
Member

adangel commented Jun 14, 2019

We could add an exception for methods, annotated with TestNG's @Test annotation.

@adangel adangel added the an:enhancement An improvement on existing features / rules label Jun 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
None yet
Development

No branches or pull requests

2 participants