Skip to content

Only convert assertInstanceOf used as a statement in JUnitAssertInstanceOfToAssertThat - #1222

Merged
timtebeek merged 1 commit into
mainfrom
tim/assert-instance-of-statement-only
Oct 24, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/assert-instance-of-statement-only

Conversation

@timtebeek

@timtebeek timtebeek commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

JUnit's assertInstanceOf returns the cast value, while AssertJ's isInstanceOf returns the assert, so converting a call whose result is used produced code that doesn't compile:

Integer i = assertInstanceOf(Integer.class, o);
// became
Integer i = assertThat(o).isInstanceOf(Integer.class);

The recipe now only converts calls whose result is discarded: a statement in a block, or the body of a lambda whose functional interface returns void (such as assertAll(() -> assertInstanceOf(..)), which keeps converting). Variable initializers, returns, method arguments and value-returning lambdas are left alone. The void check reuses returnsVoid from PlaceAssertJDescriptionBeforeAssertion, now package-private, instead of copying it. The new no-change tests are grouped in a NoChange nested class, and the existing canBeRerun moved there too.

Verified with ./gradlew test --tests 'org.openrewrite.java.testing.assertj.*' --tests 'org.openrewrite.java.testing.testng.TestNgToAssertJTest': 508 tests, 0 failures. No test loads JUnitToAssertj directly; it is covered through the Assertj composite in AssertJBestPracticesTest and TestNgToAssertJTest.

Fixes item D6 of #1211.

…stanceOfToAssertThat`

JUnit's `assertInstanceOf` returns the cast value, but AssertJ's `isInstanceOf` returns the assert, so converting a call whose result is used broke compilation: `Integer i = assertInstanceOf(Integer.class, o);` became `Integer i = assertThat(o).isInstanceOf(Integer.class);`. The recipe now leaves the call alone unless its result is discarded, i.e. it is a statement in a block or the body of a lambda whose functional interface returns `void`.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit a07ab38 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/assert-instance-of-statement-only branch October 24, 2026 13:41
@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