Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate assertEquals with array parameter to assertArrayEquals #384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
doAfterVisit(new ChangeMethodTargetToStatic("org.junit.Assert " + m.getSimpleName() + "(..)",
"org.junit.jupiter.api.Assertions", null, null, true)
.getVisitor());

List<Expression> args = m.getArguments();
Expression firstArg = args.get(0);
// Suppress arg-switching for Assertions.assertEquals(String, String)
if (args.size() == 2) {
if ("assertSame".equals(m.getSimpleName()) ||
"assertNotSame".equals(m.getSimpleName()) ||
"assertEquals".equals(m.getSimpleName()) ||
"assertNotEquals".equals(m.getSimpleName())) {
"assertNotSame".equals(m.getSimpleName()) ||
"assertEquals".equals(m.getSimpleName()) ||
"assertNotEquals".equals(m.getSimpleName())) {
return m;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ recipeList:
- org.openrewrite.java.testing.junit5.UseMockitoExtension
- org.openrewrite.java.testing.junit5.UseTestMethodOrder
- org.openrewrite.java.testing.junit5.MigrateJUnitTestCase
- org.openrewrite.java.ChangeMethodName:
methodPattern: "org.junit.Assert assertEquals(.., Object[], Object[])"
newMethodName: assertArrayEquals
- org.openrewrite.java.testing.junit5.AssertToAssertions
- org.openrewrite.java.testing.junit5.CategoryToTag
- org.openrewrite.java.testing.junit5.CleanupJUnitImports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,33 @@ void upgradeMavenPluginVersions() {
);
}


// edge case for deprecated use of assertEquals
// https://junit.org/junit4/javadoc/4.13/org/junit/Assert.html#assertEquals(java.lang.Object%5B%5D,%20java.lang.Object%5B%5D)
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/pull/384")
@Test
void assertEqualsWithArrayArgumentToAssertArrayEquals() {
//language=java
rewriteRun(
java(
"""
import org.junit.Assert;

class MyTest {
void test() {
Assert.assertEquals(new Object[1], new Object[1]);
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

class MyTest {
void test() {
Assertions.assertArrayEquals(new Object[1], new Object[1]);
}
}
"""
)
);
}
}