Skip to content

[java] Fix #7119: SimplifiableTestAssertion: Report more cases - #7129

Merged
adangel merged 4 commits into
pmd:mainfrom
Akhil-1527:issue-7119
Oct 23, 2026
Merged

adangel merged 4 commits into
pmd:mainfrom
Akhil-1527:issue-7119

Conversation

@Akhil-1527

Copy link
Copy Markdown

Describe the PR

Adds the cases from #7119 to SimplifiableTestAssertion:

  • assertTrue(a instanceof A) may use assertInstanceOf. This is only reported when the assertion class declares assertInstanceOf (JUnit 5.8 and later), and not for assertFalse.
  • assertEquals(null, a) may use assertNull, and assertNotEquals(null, a) may use assertNotNull.
  • assertEquals with two different compile-time constants always fails, so it may use fail. To avoid false positives, both constants need the same type (assertEquals(1, 1L) passes), and floating point overloads are skipped, since they may compare with a delta.

It also fixes the argument indices mentioned in the issue comments. The rule took the last argument as the condition of assertTrue/assertFalse, and the last two arguments as the values compared by assertEquals/assertNotEquals. That only fits JUnit 3 and 4, which take the message first. JUnit 5 and TestNG take it last, so calls like assertTrue(a == null, "message") or assertEquals(true, a, "message") were missed. The condition is now the boolean parameter, and the compared values are the first two arguments, unless a JUnit 3 or 4 call starts with a message. The JUnit 5 overloads that take a BooleanSupplier are still skipped.

Last, the suggestion for assertTrue(a != null) was assertNonNull, which doesn't exist. It's now assertNotNull.

One existing test changes. In "JUnit Test contains assertEquals with Boxed booleans as param", JUnit 4 takes the first argument as the message, so the call compares "a String value" with "they should be equal!" and can never pass. It's now reported with fail, and I added a comment to the test. TestNG isn't on the test classpath of pmd-java, so the new tests cover JUnit 3, 4 and 5. TestNG takes the message last like JUnit 5, and because of its String overloads, like assertEquals(String actual, String expected, String message), a leading message is only assumed for the JUnit 3 and 4 classes.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Report assertTrue(a instanceof A) when the assertion class has
assertInstanceOf, assertEquals and assertNotEquals with null, and
assertEquals with two different constants, which always fails.

Also take the condition and the compared values from the right
arguments when JUnit 5 or TestNG put the message last, and suggest
assertNotNull instead of assertNonNull.

Fixes pmd#7119
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 31 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-23 14:34:55+00:00 for f306f3b)

@adangel adangel added this to the 7.29.0 milestone Oct 23, 2026
@adangel

adangel commented Oct 23, 2026

Copy link
Copy Markdown
Member

Thanks!

@adangel
adangel merged commit 8d4b7b9 into pmd:main Oct 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants