Repository navigation
[java] Fix #7119: SimplifiableTestAssertion: Report more cases - #7129
Merged
Merged
Conversation
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
Contributor
|
Compared to main: (comment created at 2026-10-23 14:34:55+00:00 for f306f3b) |
zbynek
approved these changes
Oct 18, 2026
adangel
reviewed
Oct 23, 2026
adangel
approved these changes
Oct 23, 2026
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the PR
Adds the cases from #7119 to
SimplifiableTestAssertion:assertTrue(a instanceof A)may useassertInstanceOf. This is only reported when the assertion class declaresassertInstanceOf(JUnit 5.8 and later), and not forassertFalse.assertEquals(null, a)may useassertNull, andassertNotEquals(null, a)may useassertNotNull.assertEqualswith two different compile-time constants always fails, so it may usefail. 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 byassertEquals/assertNotEquals. That only fits JUnit 3 and 4, which take the message first. JUnit 5 and TestNG take it last, so calls likeassertTrue(a == null, "message")orassertEquals(true, a, "message")were missed. The condition is now thebooleanparameter, 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 aBooleanSupplierare still skipped.Last, the suggestion for
assertTrue(a != null)wasassertNonNull, which doesn't exist. It's nowassertNotNull.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 withfail, 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 itsStringoverloads, likeassertEquals(String actual, String expected, String message), a leading message is only assumed for the JUnit 3 and 4 classes.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)