Skip to content

Commit c5c225c

Browse files
TestNG matching for RemoveUnneededAssertion (#549)
* Adding matching for TestNG `assertTrue(..)` and `assertFalse(..)` for `RemoveUnneededAssertion` * Nesting the tests into groups of which library of assertions are being used.
1 parent 74bacdf commit c5c225c

File tree

3 files changed

+280
-165
lines changed

3 files changed

+280
-165
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies {
3636
testImplementation("org.jetbrains:annotations:24.+")
3737
testImplementation("org.junit-pioneer:junit-pioneer:2.+")
3838
testImplementation("junit:junit:4.13.2")
39+
testImplementation("org.testng:testng:7.+")
3940

4041
testImplementation("com.google.code.gson:gson:latest.release")
4142

src/main/java/org/openrewrite/staticanalysis/RemoveUnneededAssertion.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class RemoveUnneededAssertion extends Recipe {
4444
private static final MethodMatcher JUNIT_ASSERT_MESSAGE_TRUE_MATCHER = new MethodMatcher("org.junit.Assert assertTrue(String, boolean)");
4545
private static final MethodMatcher JUNIT_ASSERT_MESSAGE_FALSE_MATCHER = new MethodMatcher("org.junit.Assert assertFalse(String, boolean)");
4646

47+
// TestNG
48+
private static final MethodMatcher TEST_NG_ASSERT_TRUE_MATCHER = new MethodMatcher("org.testng.Assert assertTrue(..)");
49+
private static final MethodMatcher TEST_NG_ASSERT_FALSE_MATCHER = new MethodMatcher("org.testng.Assert assertFalse(..)");
50+
4751
@Override
4852
public String getDisplayName() {
4953
return "Remove unneeded assertions";
@@ -63,6 +67,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6367
new UsesMethod<>(JUNIT_ASSERT_FALSE_MATCHER),
6468
new UsesMethod<>(JUNIT_ASSERT_MESSAGE_TRUE_MATCHER),
6569
new UsesMethod<>(JUNIT_ASSERT_MESSAGE_FALSE_MATCHER),
70+
new UsesMethod<>(TEST_NG_ASSERT_TRUE_MATCHER),
71+
new UsesMethod<>(TEST_NG_ASSERT_FALSE_MATCHER),
6672
new JavaIsoVisitor<ExecutionContext>() {
6773
@Override
6874
public J.Assert visitAssert(J.Assert _assert, ExecutionContext ctx) {
@@ -84,6 +90,8 @@ public J.Assert visitAssert(J.Assert _assert, ExecutionContext ctx) {
8490
matchers.put(JUNIT_ASSERT_FALSE_MATCHER, isFalse);
8591
matchers.put(JUNIT_ASSERT_MESSAGE_TRUE_MATCHER, args -> J.Literal.isLiteralValue(args.get(1), true));
8692
matchers.put(JUNIT_ASSERT_MESSAGE_FALSE_MATCHER, args -> J.Literal.isLiteralValue(args.get(1), false));
93+
matchers.put(TEST_NG_ASSERT_TRUE_MATCHER, isTrue);
94+
matchers.put(TEST_NG_ASSERT_FALSE_MATCHER, isFalse);
8795

8896
return Preconditions.check(constraints, new JavaIsoVisitor<ExecutionContext>() {
8997
@Override

0 commit comments

Comments
 (0)