Skip to content

Commit

Permalink
Adapt JUnitAssertEqualsToAssertThat for TestNG
Browse files Browse the repository at this point in the history
  • Loading branch information
ssheikin committed Jun 4, 2024
1 parent eaa1e42 commit c08c364
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertEqualsToAssertThatVisitor());
return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertEqualsToAssertThatVisitor());
}

public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor<ExecutionContext> {
Expand All @@ -60,23 +60,23 @@ public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor<Execu
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_EQUALS = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertEquals(..)");
private static final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertEquals(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (!JUNIT_ASSERT_EQUALS.matches(method)) {
if (!TESTNG_ASSERT_EQUALS.matches(method)) {
return method;
}

List<Expression> args = method.getArguments();
Expression expected = args.get(0);
Expression actual = args.get(1);
Expression expected = args.get(1);
Expression actual = args.get(0);

//always add the import (even if not referenced)
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);

// Remove import for "org.junit.jupiter.api.Assertions" if no longer used.
maybeRemoveImport("org.junit.jupiter.api.Assertions");
// Remove import for "org.testng.Assert" if no longer used.
maybeRemoveImport("org.testng.Assert");

if (args.size() == 2) {
return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});")
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,20 @@ recipeList:
version: 3.x
onlyIfUsing: org.assertj.core.api.Assertions
acceptTransitive: true

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.assertj.TestNgToAssertj
displayName: Migrate TestNG asserts to AssertJ
description: AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.testng.Assert` to `org.assertj.core.api.Assertions`.
tags:
- testing
- assertj
recipeList:
- org.openrewrite.java.testing.assertj.TestNgAssertEqualsToAssertThat
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.assertj
artifactId: assertj-core
version: 3.x
onlyIfUsing: org.assertj.core.api.Assertions
acceptTransitive: true

0 comments on commit c08c364

Please sign in to comment.