Skip to content

Keep JUnit deltas when converting assertEquals, assertNotEquals and assertArrayEquals to AssertJ - #1216

Merged
timtebeek merged 2 commits into
mainfrom
tim/assertj-junit-delta-overloads
Oct 24, 2026
Merged

timtebeek merged 2 commits into
mainfrom
tim/assertj-junit-delta-overloads

Conversation

@timtebeek

Copy link
Copy Markdown
Member

JUnitAssertEqualsToAssertThat dropped any delta when the actual was an int or long, so assertEquals(100, elapsed, 10) became assertThat(elapsed).isEqualTo(100) and a test that tolerated a little jitter started failing. With a fractional expected value, as in assertEquals(1.5, count, 0.5), the result was an isEqualTo that could never pass. JUnit widens the actual to the float or double delta type, so the recipe now casts it the same way and keeps the tolerance: assertThat((float) elapsed).isCloseTo(100, within((float) 10)). The delta is only dropped when it is a literal zero and both values are int or long, as in the existing assertEquals(50L, getSummation(), 0.0) case.

JUnitAssertNotEqualsToAssertThat and JUnitAssertArrayEqualsToAssertThat still decided between a delta and a message from the third argument's own type, the problem #911 fixed for assertEquals only. So assertNotEquals(0.0, d, 0) became assertThat(d).as(0).isNotEqualTo(0.0), and assertNotEquals(5, count, 1.0) called an isNotCloseTo(int, Offset<Double>) that doesn't exist. Both now look at the resolved parameter type and share the cast handling, giving assertThat(d).isNotCloseTo(0.0, within((double) 0)) and assertThat((double) count).isNotCloseTo(5, within(1.0)). I compiled the new test outputs against AssertJ 3.27.4 to check that these overloads exist.

Fixes item D2 of #1211.

…tThat`

`assertEquals(100, elapsed, 10)` with a `long elapsed` became `assertThat(elapsed).isEqualTo(100)`, silently dropping the tolerance, and `assertEquals(1.5, count, 0.5)` became an `isEqualTo` that can never pass. JUnit widens the actual to the `float` or `double` delta type, so the recipe now casts it the same way and keeps the tolerance, as in `assertThat((float) elapsed).isCloseTo(100, within((float) 10))`. The delta is only dropped when it is a literal zero and both values are `int` or `long`.
…s` and `assertArrayEquals`

Like `assertEquals` before #911, `JUnitAssertNotEqualsToAssertThat` and `JUnitAssertArrayEqualsToAssertThat` decided whether the third argument was a delta from that argument's own type. So `assertNotEquals(0.0, d, 0)` became `.as(0)`, and `assertNotEquals(5, count, 1.0)` called an `isNotCloseTo(int, Offset<Double>)` that doesn't exist. Both now use the resolved parameter type and cast the actual and delta where JUnit widened them, sharing the helpers with `JUnitAssertEqualsToAssertThat`.
@timtebeek timtebeek changed the title Keep JUnit deltas when converting to AssertJ, and tell them from messages by the resolved overload Keep JUnit deltas when converting assertEquals, assertNotEquals and assertArrayEquals to AssertJ Oct 24, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit 06491a7 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/assertj-junit-delta-overloads branch October 24, 2026 11:30
@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