Skip to content

Only replace assertNotEquals(false, <boolean>) when the second argument is a primitive boolean - #1220

Merged
timtebeek merged 1 commit into
mainfrom
tim/assert-not-equals-boolean-type-check
Oct 24, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/assert-not-equals-boolean-type-check

Conversation

@timtebeek

@timtebeek timtebeek commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

AssertNotEqualsBooleanToAssertBoolean rewrote assertNotEquals(false, x) to assertTrue(x) without checking the type of x, unlike its sibling AssertEqualsBooleanToAssertBoolean. With a Map<String, Object> map, assertNotEquals(false, map.get("k")) became assertTrue(map.get("k")), which does not compile.

The recipe now only rewrites when the second argument is a primitive boolean, the same check the sibling uses. Boxed java.lang.Boolean is left alone on purpose: assertNotEquals(false, (Boolean) null) passes, but assertTrue((Boolean) null) throws a NullPointerException on unboxing, so the rewrite would turn a passing test into a failing one. This is stricter than the sibling needs to be, but it matches what the sibling already does with Boolean. New no-change tests cover the Object and Boolean cases. Both fail without the fix.

Verified with ./gradlew test --tests 'org.openrewrite.java.testing.cleanup.*' --tests 'org.openrewrite.java.testing.junit5.CleanupAssertionsTest': 162 tests, 0 failures.

This covers only the probed part of C7; the other C7 entries (AssertionsArgumentOrder, SimplifyTestThrows and the suspected ones) are left for separate PRs.

Fixes item C7 of #1211.

…ment is a primitive `boolean`

`AssertNotEqualsBooleanToAssertBoolean` rewrote `assertNotEquals(false, x)` to `assertTrue(x)` for any `x`. With a `Map<String, Object>`, `assertNotEquals(false, map.get("k"))` became `assertTrue(map.get("k"))`, which does not compile, and with a `Boolean` holding `null` a passing assertion became an unboxing `NullPointerException`. Require a primitive `boolean`, as `AssertEqualsBooleanToAssertBoolean` already does.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit f920ae1 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/assert-not-equals-boolean-type-check branch October 24, 2026 13:47
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant