Skip to content

Only use hasSameSizeAs in SimplifyHasSizeAssertion when the assert type has a matching overload - #1243

Merged
timtebeek merged 2 commits into
mainfrom
tim/d4-issue-1211-fix
Oct 25, 2026
Merged

timtebeek merged 2 commits into
mainfrom
tim/d4-issue-1211-fix

Conversation

@timtebeek

Copy link
Copy Markdown
Member

SimplifyHasSizeAssertion turned assertThat(list).hasSize(str.length()) into assertThat(list).hasSameSizeAs(str). That compiles, but binds to hasSameSizeAs(Object array) and fails at runtime because str is not an array. The same happened for a Map size on an iterable or array assert, and for a string length on any assert other than a char sequence one.

Every AssertJ assert with hasSize(int) also has hasSameSizeAs(Iterable<?>) and hasSameSizeAs(Object array). Only AbstractMapAssert adds hasSameSizeAs(Map<?, ?>), and only AbstractCharSequenceAssert adds hasSameSizeAs(CharSequence) (checked with javap against assertj-core 3.27.7). The recipe now rewrites map.size() only on map asserts and str.length() only on char sequence asserts. Iterable sizes and array lengths are still rewritten on all of them.

Verified with ./gradlew test --offline --tests 'org.openrewrite.java.testing.assertj.*': 495 tests, all passing. The new negative test fails on main.

Fixes item D4 of #1211.

@timtebeek
timtebeek merged commit b020f04 into main Oct 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/d4-issue-1211-fix branch October 25, 2026 22:44
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 25, 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