Skip to content

Commit cb3c8c0

Browse files
committed
Only convert when actual is of type boolean too
Fixes #587
1 parent 8145e8b commit cb3c8c0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsBooleanToAssertBoolean.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5757
@Override
5858
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
5959
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
60-
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
60+
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi) &&
61+
JavaType.Primitive.Boolean.equals(mi.getArguments().get(1).getType())) {
6162
StringBuilder sb = new StringBuilder();
6263
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
6364
? "assertTrue" : "assertFalse";

src/test/java/org/openrewrite/java/testing/cleanup/AssertEqualsBooleanToAssertBooleanTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,23 @@ void test() {
105105
)
106106
);
107107
}
108+
109+
@Test
110+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/587")
111+
void assertTrueWithNonBoolean() {
112+
rewriteRun(
113+
spec -> spec.recipe(new AssertEqualsBooleanToAssertBoolean()),
114+
// language=java
115+
java(
116+
"""
117+
import static org.junit.jupiter.api.Assertions.assertEquals;
118+
class Main {
119+
void foo() {
120+
assertEquals(true, new Object());
121+
}
122+
}
123+
"""
124+
)
125+
);
126+
}
108127
}

0 commit comments

Comments
 (0)