Skip to content

[java] SimplifiableTestAssertion: False negatives for assertInstanceOf, assertNull, assertNonNull #7119

Closed

Description

@zbynek

Several simplifications of assertions are currently not covered by the SimplifiableTestAssertion rule:
https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#simplifiabletestassertion

Code Sample:

assertTrue(a instanceof A); // violation
assertInstanceOf(a, A.class); // good

assertEquals(null, a); // violation
assertNull(a); // good

assertNotEquals(null, a); // violation
assertNonNull(a); // good

assertEquals(true, false); // violation, same for any two different compile time constants
fail("message"); // good

Activity

  1. added
    a:new-rule
    on Sep 28, 2026
  2. pzygielo commented on Sep 28, 2026

    @pzygielo
  3. changed the title [-][java] new rule: SimplifieableAssertion[/-] [+][java] new rule: SimplifiableAssertion[/+] on Sep 28, 2026
  4. adangel commented on Sep 28, 2026

    @adangel
  5. changed the title [-][java] new rule: SimplifiableAssertion[/-] [+][java] add more cases to SimplifiableAssertion[/+] on Sep 28, 2026
  6. changed the title [-][java] add more cases to SimplifiableAssertion[/-] [+][java] add more cases to SimplifiableTestAssertion[/+] on Sep 28, 2026
  7. zbynek commented on Sep 28, 2026

    @zbynek
    ContributorAuthor

    @adangel thanks, I updated the description as an amendment to the existing rule, only listing the cases that are not covered yet.

    Also, if someone works on that rule, it might be worth checking that the argument indices are selected correctly (the rule seems to expect that the last two arguments of assertEquals are actual and expected in some order, but that's not true for Assertions.assertEquals(expected, actual, message)).

  8. added
    a:false-negative
    and removed
    a:new-rule
    on Sep 28, 2026
  9. dan-s1 commented on Oct 22, 2026

    @dan-s1

    @zbynek Thank you for creating this ticket! In the Apache NIFI code base, we had replaced all
    assertTrue(a instanceof A); withassertInstanceOf(a, A.class); yet it got introduced again with a later PR. Having this in PMD would help with regression.

  10. changed the title [-][java] add more cases to SimplifiableTestAssertion[/-] [+][java] SimplifiableTestAssertion: Various false negatives[/+] on Oct 22, 2026
  11. added a commit that references this issue on Oct 23, 2026
  12. added this to the 7.29.0 milestone on Oct 23, 2026
  13. added a commit that references this issue on Oct 23, 2026
    8d4b7b9
  14. changed the title [-][java] SimplifiableTestAssertion: Various false negatives[/-] [+][java] SimplifiableTestAssertion: False negatives for assertInstanceOf, assertNull, assertNonNull[/+] on Oct 29, 2026
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

    a:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions