Refactor assertThat(x instanceof Type).isTrue() to assertThat(x).isInstanceOf(Type.class)#1035
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes AssertJ: Refactor assertThat(x instanceof Type).isTrue() to assertThat(x).isInstanceOf(Type.class) #1030
When the expression inside
assertThat(...)is aninstanceofcheck, the AssertJ best-practices recipe previously left it as a literal boolean port (assertThat(error instanceof RuntimeException).isTrue()), so the failure message was just "expected true but was false" rather than describing the actual type. This is theJ.InstanceOfsibling of AssertJ: RefactorassertThatwith<or>in the expression toassertThat(actual).isGreaterThan(expected)orassertThat(actual).isLessThan(expected)#814 (</>comparisons): the argument ofassertThat(...)is a non-J.MethodInvocationboolean expression that the existingSimplifyChainedAssertJAssertionskips.Change
New recipe
SimplifyAssertJInstanceOfAssertion, added to theorg.openrewrite.java.testing.assertj.Assertjrecipe list:becomes
It also handles:
assertThat((error instanceof RuntimeException))assertThat(!(x instanceof T)).isTrue()→isNotInstanceOf(and.isFalse()→isInstanceOf)Assertions.assertThat(...)as well as the static importString[].class)It deliberately skips Java 16+ pattern bindings (
x instanceof String s), whose variable may be referenced later.The previously commented-out
instanceofcase inAssertJBestPracticesTestis now enabled to confirm end-to-end coverage through the fullAssertjrecipe.