Skip to content

Only adopt hasSeconds for isEqualTo in AdoptAssertJDurationAssertions - #1215

Merged
timtebeek merged 3 commits into
mainfrom
tim/duration-assertions-equality-only
Oct 24, 2026
Merged

timtebeek merged 3 commits into
mainfrom
tim/duration-assertions-equality-only

Conversation

@timtebeek

Copy link
Copy Markdown
Member

AdoptAssertJDurationAssertions could change what a test checks. It turned comparisons on Duration#getSeconds() into equality, so assertThat(d.getSeconds()).isGreaterThan(5) became assertThat(d).hasSeconds(5). It mapped getNano(), the nanosecond-of-second part, to hasNanos, which compares toNanos() of the whole duration. And it "simplified" units upwards, so hasMillis(5000) became hasSeconds(5), which also passes for 5999 ms. Along the way a zero argument ended up as hasDays(0), and values outside int range threw from Math.toIntExact.

The recipe now only adopts hasSeconds for assertThat(d.getSeconds()).isEqualTo(x). It leaves getNano() assertions and the existing unit assertions as written. Zero comparisons such as isGreaterThan(0) still become isPositive(), isNegative() or isZero(), now for any Duration expression and not only a method call.

Fixes item D1 of #1211.

…nAssertions`

`Duration#getNano()` returns only the nanosecond-of-second part, while AssertJ `DurationAssert#hasNanos` compares `toNanos()` of the whole duration. Rewriting `assertThat(d.getNano()).isEqualTo(1)` to `assertThat(d).hasNanos(1)` therefore made a passing assertion fail for any duration of a second or more, so the recipe now leaves `getNano()` assertions alone.
…`AdoptAssertJDurationAssertions`

The `getSeconds()` branch did not check which assertion it was replacing, so `assertThat(d.getSeconds()).isGreaterThan(5)` became `assertThat(d).hasSeconds(5)`, turning a comparison into an equality check. The zero handling also only recognised a method call as the `Duration`, so `assertThat(d.getSeconds()).isGreaterThan(0)` on a variable fell through to `hasSeconds(0)`. The recipe now only adopts `hasSeconds` for `isEqualTo`, and turns zero comparisons into `isPositive()`, `isNegative()` or `isZero()` for any `Duration` expression.
…ions`

AssertJ `hasSeconds`, `hasMinutes` and the other unit assertions compare the duration truncated to that unit, so rewriting `hasMillis(5000)` to `hasSeconds(5)` weakened the assertion to also pass for 5999 ms. The conversion also turned a zero argument into `hasDays(0)` and threw an `ArithmeticException` from `Math.toIntExact` for values beyond `int` range. The recipe now leaves the unit assertions as written.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit 2936f17 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/duration-assertions-equality-only branch October 24, 2026 11:17
@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