Skip to content

Commit 4de7603

Browse files
committed
Prevent missing types in AssertTrueInstanceofToAssertInstanceOf
Fixes #730
1 parent 258cc3f commit 4de7603

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOf.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.java.MethodMatcher;
2525
import org.openrewrite.java.tree.Expression;
2626
import org.openrewrite.java.tree.J;
27+
import org.openrewrite.java.tree.TypedTree;
2728

2829
public class AssertTrueInstanceofToAssertInstanceOf extends Recipe {
2930
@Override
@@ -46,7 +47,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
4647
MethodMatcher junit5Matcher = new MethodMatcher("org.junit.jupiter.api.Assertions assertTrue(boolean, ..)");
4748
MethodMatcher junit4Matcher = new MethodMatcher("org.junit.Assert assertTrue(.., boolean)");
4849

49-
J clazz;
50+
TypedTree clazz;
5051
Expression expression;
5152
Expression reason;
5253

@@ -64,7 +65,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6465
if (argument instanceof J.InstanceOf) {
6566
J.InstanceOf instanceOf = (J.InstanceOf) argument;
6667
expression = instanceOf.getExpression();
67-
clazz = instanceOf.getClazz();
68+
clazz = (TypedTree) instanceOf.getClazz();
6869
} else {
6970
return mi;
7071
}
@@ -84,7 +85,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
8485
if (argument instanceof J.InstanceOf) {
8586
J.InstanceOf instanceOf = (J.InstanceOf) argument;
8687
expression = instanceOf.getExpression();
87-
clazz = instanceOf.getClazz();
88+
clazz = (TypedTree) instanceOf.getClazz();
8889
} else {
8990
return mi;
9091
}
@@ -97,6 +98,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
9798
.builder("assertInstanceOf(#{}.class, #{any(java.lang.Object)}" + (reason != null ? ", #{any(java.lang.String)})" : ")"))
9899
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5", "junit-4"))
99100
.staticImports("org.junit.jupiter.api.Assertions.assertInstanceOf")
101+
.imports(String.valueOf(clazz.getType()))
100102
.build();
101103

102104
J.MethodInvocation methodd = reason != null ?

src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ATest {
5050
@Test
5151
void testJUnit5() {
5252
List<String> list = new ArrayList<>();
53-
assertTrue(list instanceof Iterable);
53+
assertTrue(list instanceof List);
5454
}
5555
}
5656
""",
@@ -65,7 +65,7 @@ class ATest {
6565
@Test
6666
void testJUnit5() {
6767
List<String> list = new ArrayList<>();
68-
assertInstanceOf(Iterable.class, list);
68+
assertInstanceOf(List.class, list);
6969
}
7070
}
7171
"""

0 commit comments

Comments
 (0)