Skip to content

Refactor assertThat(x instanceof Type).isTrue() to assertThat(x).isInstanceOf(Type.class)#1035

Merged
timtebeek merged 2 commits into
mainfrom
tim/issue-1030
Jun 20, 2026
Merged

Refactor assertThat(x instanceof Type).isTrue() to assertThat(x).isInstanceOf(Type.class)#1035
timtebeek merged 2 commits into
mainfrom
tim/issue-1030

Conversation

@timtebeek

@timtebeek timtebeek commented Jun 20, 2026

Copy link
Copy Markdown
Member

Change

New recipe SimplifyAssertJInstanceOfAssertion, added to the org.openrewrite.java.testing.assertj.Assertj recipe list:

assertThat(error instanceof RuntimeException).isTrue();
assertThat(error instanceof RuntimeException).isFalse();

becomes

assertThat(error).isInstanceOf(RuntimeException.class);
assertThat(error).isNotInstanceOf(RuntimeException.class);

It also handles:

  • the parenthesized form assertThat((error instanceof RuntimeException))
  • the negated form assertThat(!(x instanceof T)).isTrue()isNotInstanceOf (and .isFalse()isInstanceOf)
  • qualified Assertions.assertThat(...) as well as the static import
  • array types (String[].class)

It deliberately skips Java 16+ pattern bindings (x instanceof String s), whose variable may be referenced later.

The previously commented-out instanceof case in AssertJBestPracticesTest is now enabled to confirm end-to-end coverage through the full Assertj recipe.

Convert `assertThat(x instanceof Type).isTrue()` to the dedicated
`assertThat(x).isInstanceOf(Type.class)` (and `.isFalse()` / negated
forms to `isNotInstanceOf`), so failures describe the actual type
instead of just "expected true but was false".

Handles parenthesized and `!`-negated arguments, qualified and
statically imported `assertThat`, array types, and skips Java 16+
pattern bindings whose variable may be used later.

Fixes #1030
@timtebeek timtebeek merged commit 286a3d7 into main Jun 20, 2026
1 check passed
@timtebeek timtebeek deleted the tim/issue-1030 branch June 20, 2026 12:05
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 20, 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.

AssertJ: Refactor assertThat(x instanceof Type).isTrue() to assertThat(x).isInstanceOf(Type.class)

1 participant