Skip to content

Commit

Permalink
Apply special migration for TestNG assertNotEquals with arrays
Browse files Browse the repository at this point in the history
As with `assertEquals`, this is something that the TestNG implementation handles
dedicated, while the JUnit 5 assertions don't.

`Assert.assertNotEquals(new Object[] {"x", "y"}, new Object[] {"x", "y"})` → fails (correct)

but

`Assertions.assertNotEquals(new Object[] {"x", "y"}, new Object[] {"x", "y"})` → passes (incorrect)

The workaround here is:

`Assertions.assertNotEquals(Arrays.toString(new Object[] {"x", "y"}), Arrays.toString(new Object[] {"x", "y"})`
  • Loading branch information
Philzen committed Jun 12, 2024
1 parent 383984f commit 6f4e1a0
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.openrewrite.java.template.RecipeDescriptor;
import org.testng.Assert;

import java.util.Arrays;

@RecipeDescriptor(
name = "Migrate TestNG Asserts to Jupiter",
description = "Migrate all TestNG Assertions to JUnit Jupiter Assertions."
Expand Down Expand Up @@ -70,8 +72,7 @@ public static class MigrateArrayAssertEqualsWithMsg {
@RecipeDescriptor(
name = "Replace `Assert#assertEquals(?, ?, String)` for primitive values, boxed types and other non-array objects",
description = "Replace `org.testng.Assert#assertEquals(?, ?, String)` with `org.junit.jupiter.api.Assertions#assertEquals(?, ?, String)`."
+ "Always run *after* `MigrateArrayAssertEqualsWithMsg`."

+ "Always run *after* `MigrateArrayAssertEqualsWithMsgRecipe`."
)
public static class MigrateValueAssertEqualsWithMsg {

Expand All @@ -86,35 +87,63 @@ void after(Object actual, Object expected, String msg) {
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(?, ?)`",
description = "Replace `org.testng.Assert#assertEquals(?, ?)` with `org.junit.jupiter.api.Assertions#assertEquals(?, ?)`."
name = "Replace `Assert#assertNotEquals(Object[], Object[])`",
description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]))`."
)
public static class MigrateAssertArrayNotEquals {

@BeforeTemplate void before(Object[] actual, Object[] expected) {
Assert.assertNotEquals(actual, expected);
}

@AfterTemplate void after(Object[] actual, Object[] expected) {
Assertions.assertNotEquals(Arrays.toString(expected), Arrays.toString(actual));
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(?, ?)`",
description = "Replace `org.testng.Assert#assertNotEquals(?, ?)` with `org.junit.jupiter.api.Assertions#assertNotEquals(?, ?)`."
+ "Always run *after* `MigrateAssertArrayNotEqualsRecipe`."
)
public static class MigrateAssertNotEqual {

@BeforeTemplate
void before(Object actual, Object expected) {
@BeforeTemplate void before(Object actual, Object expected) {
Assert.assertNotEquals(actual, expected);
}

@AfterTemplate
void after(Object actual, Object expected) {
@AfterTemplate void after(Object actual, Object expected) {
Assertions.assertNotEquals(expected, actual);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(?, ?, String)`",
description = "Replace `org.testng.Assert#assertEquals(?, ?, String)` with `org.junit.jupiter.api.Assertions#assertEquals(?, ?, String)`."
name = "Replace `Assert#assertNotEquals(Object[], Object[], String)`",
description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]), String)`."
)
public static class MigrateAssertArrayNotEqualsWithMsg {

@BeforeTemplate void before(Object[] actual, Object[] expected, String message) {
Assert.assertNotEquals(actual, expected, message);
}

@AfterTemplate void after(Object[] actual, Object[] expected, String message) {
Assertions.assertNotEquals(Arrays.toString(expected), Arrays.toString(actual), message);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(?, ?, String)`",
description = "Replace `org.testng.Assert#assertNotEquals(?, ?, String)` with `org.junit.jupiter.api.Assertions#assertNotEquals(?, ?, String)`."
+ "Always run *after* `MigrateAssertArrayNotEqualsWithMsgRecipe`."
)
public static class MigrateAssertNotEqualWithMsg {

@BeforeTemplate
void before(Object actual, Object expected, String msg) {
@BeforeTemplate void before(Object actual, Object expected, String msg) {
Assert.assertNotEquals(actual, expected, msg);
}

@AfterTemplate
void after(Object actual, Object expected, String msg) {
@AfterTemplate void after(Object actual, Object expected, String msg) {
Assertions.assertNotEquals(expected, actual, msg);
}
}
Expand Down
Loading

0 comments on commit 6f4e1a0

Please sign in to comment.