Skip to content

Commit

Permalink
AssertToAssertions should only convert methods from exact class (#516)
Browse files Browse the repository at this point in the history
* AssertToAssertions should only convert methods from exact class

Fixes #515

* Remove trailing whitespace in text blocks
  • Loading branch information
timtebeek authored May 19, 2024
1 parent 33da4d3 commit 4700bd2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

private static boolean isJunitAssertMethod(J.MethodInvocation method) {
if (method.getMethodType() != null && TypeUtils.isAssignableTo(ASSERTION_TYPE, method.getMethodType().getDeclaringType())) {
if (method.getMethodType() != null && TypeUtils.isOfType(ASSERTION_TYPE, method.getMethodType().getDeclaringType())) {
return !"assertThat".equals(method.getSimpleName());
}
if (method.getMethodType() == null && JUNIT_ASSERT_METHOD_NAMES.contains(method.getSimpleName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;
Expand Down Expand Up @@ -55,7 +56,7 @@ String getField() {
java(
"""
import static org.junit.Assert.assertEquals;
class MyTest {
void foo() {
Entity entity = new Entity();
Expand All @@ -66,7 +67,7 @@ void foo() {
""",
"""
import static org.junit.jupiter.api.Assertions.assertEquals;
class MyTest {
void foo() {
Entity entity = new Entity();
Expand Down Expand Up @@ -147,7 +148,7 @@ public void test() {
""",
"""
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class MyTest {
Expand All @@ -169,9 +170,9 @@ void assertWithoutMessage() {
java(
"""
import org.junit.Assert;
class MyTest {
void foo() {
Assert.assertEquals(1, 2);
Assert.assertArrayEquals(new int[]{}, new int[]{});
Expand All @@ -193,9 +194,9 @@ void foo() {
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void foo() {
Assertions.assertEquals(1, 2);
Assertions.assertArrayEquals(new int[]{}, new int[]{});
Expand Down Expand Up @@ -226,9 +227,9 @@ void staticAssertWithoutMessage() {
java(
"""
import static org.junit.Assert.*;
class MyTest {
void foo() {
assertEquals(1, 2);
assertArrayEquals(new int[]{}, new int[]{});
Expand All @@ -243,9 +244,9 @@ void foo() {
""",
"""
import static org.junit.jupiter.api.Assertions.*;
class MyTest {
void foo() {
assertEquals(1, 2);
assertArrayEquals(new int[]{}, new int[]{});
Expand All @@ -269,9 +270,9 @@ void assertWithMessage() {
java(
"""
import org.junit.Assert;
class MyTest {
void foo() {
Assert.assertEquals("One is one", 1, 1);
Assert.assertArrayEquals("Empty is empty", new int[]{}, new int[]{});
Expand All @@ -290,9 +291,9 @@ void foo() {
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void foo() {
Assertions.assertEquals(1, 1, "One is one");
Assertions.assertArrayEquals(new int[]{}, new int[]{}, "Empty is empty");
Expand Down Expand Up @@ -326,7 +327,7 @@ void staticallyImportAssertions() {
java(
"""
import org.junit.Assert;
class Test {
void test() {
Assert.assertEquals("One is one", 1, 1);
Expand All @@ -335,7 +336,7 @@ void test() {
""",
"""
import static org.junit.jupiter.api.Assertions.assertEquals;
class Test {
void test() {
assertEquals(1, 1, "One is one");
Expand All @@ -354,7 +355,7 @@ void swapAssertTrueArgumentsWhenMessageIsBinaryExpression() {
java(
"""
import org.junit.Assert;
class Test {
void test() {
Assert.assertTrue("one" + "one", true);
Expand All @@ -363,7 +364,7 @@ void test() {
""",
"""
import org.junit.jupiter.api.Assertions;
class Test {
void test() {
Assertions.assertTrue(true, "one" + "one");
Expand Down Expand Up @@ -391,7 +392,7 @@ void testNestedPartitionStepStepReference() {
""",
"""
import static org.junit.jupiter.api.Assertions.assertNotNull;
class MyTest {
Long l = 1L;
void testNestedPartitionStepStepReference() {
Expand Down Expand Up @@ -429,7 +430,7 @@ void test(Runnable run) {
""",
"""
import static org.junit.jupiter.api.Assertions.assertThrows;
class Test {
void test(Runnable run) {
assertThrows(
Expand Down Expand Up @@ -457,7 +458,7 @@ void missingTypeInfo() {
java(
"""
import static org.junit.Assert.*;
class MyTest {
void test() {
assertNotNull(UnknownType.unknownMethod());
Expand All @@ -466,7 +467,7 @@ void test() {
""",
"""
import static org.junit.jupiter.api.Assertions.*;
class MyTest {
void test() {
assertNotNull(UnknownType.unknownMethod());
Expand All @@ -487,7 +488,7 @@ void migratesAssertStatementsWithMissingTypeInfo() {
java(
"""
import static org.junit.Assert.assertNotNull;
class MyTest {
void test() {
assertNotNull(UnknownType.unknownMethod());
Expand All @@ -497,7 +498,7 @@ void test() {
"""
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class MyTest {
void test() {
assertNotNull(UnknownType.unknownMethod());
Expand All @@ -507,4 +508,50 @@ void test() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/515")
void verifyClassExtendsAssertMethodArgumentsOrderRetained() {
//language=java
rewriteRun(
java(
"""
package foo;
import org.junit.Assert;
public class Verify extends Assert {
public static void assertContains(String expected, String actual) {
}
}
""",
SourceSpec::skip
),
java(
"""
import foo.Verify;
import org.junit.Assert;
import java.util.List;
class A {
void test(String message, String expected, String actual) {
Verify.assertContains(expected, actual);
Assert.assertEquals(message, expected, actual);
}
}
""",
"""
import foo.Verify;
import org.junit.jupiter.api.Assertions;
import java.util.List;
class A {
void test(String message, String expected, String actual) {
Verify.assertContains(expected, actual);
Assertions.assertEquals(expected, actual, message);
}
}
"""
)
);
}
}

0 comments on commit 4700bd2

Please sign in to comment.