Skip to content

Commit a0ff46d

Browse files
committed
fixes #76 AssertToAssertions isJunitAssertMethod also checks declaring type
1 parent 3bff88a commit a0ff46d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
5353

5454
public static class AssertToAssertionsVisitor extends JavaIsoVisitor<ExecutionContext> {
5555

56+
private static JavaType ASSERTION_TYPE = JavaType.buildType("org.junit.Assert");
57+
5658
@Override
5759
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
5860
doAfterVisit(new ChangeType("org.junit.Assert", "org.junit.jupiter.api.Assertions"));
@@ -86,6 +88,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
8688
}
8789

8890
private boolean isJunitAssertMethod(J.MethodInvocation method) {
91+
if (method.getType() != null && TypeUtils.isAssignableTo(ASSERTION_TYPE, method.getType().getDeclaringType())) {
92+
return true;
93+
}
8994
if (!(method.getSelect() instanceof J.Identifier)) {
9095
return false;
9196
}

src/test/kotlin/org/openrewrite/java/testing/junit5/AssertToAssertionsTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,27 @@ class AssertToAssertionsTest : JavaRecipeTest {
194194
}
195195
"""
196196
)
197+
198+
@Issue("#76")
199+
@Test
200+
fun isJUnitAssertMethodChecksDeclaringType() = assertChanged(
201+
before = """
202+
import static org.junit.Assert.assertNotNull;
203+
class A {
204+
Long l = 1L;
205+
void testNestedPartitionStepStepReference() throws Throwable {
206+
assertNotNull("message", l);
207+
}
208+
}
209+
""",
210+
after = """
211+
import static org.junit.jupiter.api.Assertions.assertNotNull;
212+
class A {
213+
Long l = 1L;
214+
void testNestedPartitionStepStepReference() throws Throwable {
215+
assertNotNull(l, "message");
216+
}
217+
}
218+
"""
219+
)
197220
}

0 commit comments

Comments
 (0)