What problem are you trying to solve?
What precondition(s) should be checked before applying this recipe?
AssertJ available.
Describe the situation before applying the recipe
class A {
void foo(Object error) {
assertThat(error instanceof RuntimeException).isTrue();
assertThat(error instanceof RuntimeException).isFalse();
}
}
Describe the situation after applying the recipe
class A {
void foo(Object error) {
assertThat(error).isInstanceOf(RuntimeException.class);
assertThat(error).isNotInstanceOf(RuntimeException.class);
}
}
(Should also handle the parenthesized form assertThat((error instanceof RuntimeException)) and the negated assertThat(!(x instanceof T)).isTrue().)
Any additional context
Are you interested in contributing this recipe?
Can help with samples/testing; happy to review a PR.
What problem are you trying to solve?
assertThat(...)is aninstanceofcheck, the AssertJ best-practices recipe leaves it as a literal boolean port instead of using the dedicatedisInstanceOfassertion — so the failure message is just "expected true but was false" instead of describing the actual type. This is the direct sibling of AssertJ: RefactorassertThatwith<or>in the expression toassertThat(actual).isGreaterThan(expected)orassertThat(actual).isLessThan(expected)#814 (</>comparisons), which now is handled; the common root cause is that the argument ofassertThat(...)is a non-J.MethodInvocationboolean expression (hereJ.InstanceOf), which the existingSimplify*recipes skip.What precondition(s) should be checked before applying this recipe?
AssertJ available.
Describe the situation before applying the recipe
Describe the situation after applying the recipe
(Should also handle the parenthesized form
assertThat((error instanceof RuntimeException))and the negatedassertThat(!(x instanceof T)).isTrue().)Any additional context
Found while migrating the TestNG suite to AssertJ (testng-team/testng#3278) via
org.openrewrite.java.testing.testng.TestNgToAssertj. I ranorg.openrewrite.java.testing.assertj.Assertj(rewrite-testing-frameworks 3.39.0) as arewriteDryRunover the test sources to confirm coverage:comparisons are now correctly handled (AssertJ: Refactor
assertThatwith<or>in the expression toassertThat(actual).isGreaterThan(expected)orassertThat(actual).isLessThan(expected)#814) —long/intoperands convert toisLessThan/isGreaterThan/isPositive/… ;method-call predicates convert (
.size()→hasSize,.isEmpty()→isEmpty/isNotEmpty, etc.) ;but
instanceofpredicates produced 0 conversions (~20 occurrences), e.g.assertThat(error instanceof RuntimeException).isTrue().Related: AssertJ: Refactor
assertThatwith<or>in the expression toassertThat(actual).isGreaterThan(expected)orassertThat(actual).isLessThan(expected)#814 (comparisons, now handled), JUnit5's assertInstanceOf is not converted to AssertJ equivalent #607 (assertInstanceOffrom JUnit 5 — a different source pattern).Are you interested in contributing this recipe?
Can help with samples/testing; happy to review a PR.