Skip to content

[java] UseObjectForClearerAPI conflicts with TestNG DataProvider #1867

Open

Description

@ThrawnCA

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

Activity

  1. adangel commented on Jun 14, 2019

    @adangel
    Member

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

  2. added
    an:enhancement
    on Jun 15, 2019
  3. UncleOwen commented on Jun 19, 2026

    @UncleOwen
    Member

    ExcessiveParameterList suffers from the same problem

  4. UncleOwen commented on Jun 19, 2026

    @UncleOwen
    Member

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

    ... and an exception for methods that override a superclass method. Nothing you can do about the long parameter list in that case. See #3741 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions