diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java index d875d10fe..cdc536a9b 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java @@ -76,6 +76,12 @@ private static class RemoveTestPrefixVisitor extends JavaIsoVisitor JUNIT5_TEST_ANNOTATIONS = Arrays.asList( + new AnnotationMatcher("@org.junit.jupiter.api.Test"), + new AnnotationMatcher("@org.junit.jupiter.api.RepeatedTest"), + new AnnotationMatcher("@org.junit.jupiter.params.ParameterizedTest"), + new AnnotationMatcher("@org.junit.jupiter.api.TestFactory"), + new AnnotationMatcher("@org.junit.jupiter.api.TestTemplate")); @Override public J.Import visitImport(J.Import _import, ExecutionContext ctx) { @@ -187,12 +193,10 @@ private boolean methodExists(JavaType.Method method, String newName) { private static boolean hasJUnit5MethodAnnotation(MethodDeclaration method) { for (J.Annotation a : method.getLeadingAnnotations()) { - if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")) { - return true; + for (AnnotationMatcher matcher : JUNIT5_TEST_ANNOTATIONS) { + if (matcher.matches(a)) { + return true; + } } } return false; diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java b/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java index c0cdb7ae2..e916d589d 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java @@ -19,6 +19,7 @@ import org.jetbrains.annotations.Contract; import org.jspecify.annotations.Nullable; import org.openrewrite.*; +import org.openrewrite.java.AnnotationMatcher; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; @@ -27,12 +28,19 @@ import java.time.Duration; import java.util.List; +import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; public class SimplifyTestThrows extends Recipe { private static final String FQN_JAVA_LANG_EXCEPTION = "java.lang.Exception"; + private static final List JUNIT5_TEST_ANNOTATIONS = asList( + new AnnotationMatcher("@org.junit.jupiter.api.Test"), + new AnnotationMatcher("@org.junit.jupiter.api.TestTemplate"), + new AnnotationMatcher("@org.junit.jupiter.api.RepeatedTest"), + new AnnotationMatcher("@org.junit.jupiter.params.ParameterizedTest"), + new AnnotationMatcher("@org.junit.jupiter.api.TestFactory")); @Getter final String displayName = "Simplify `throws` statements of tests"; @@ -96,19 +104,17 @@ private boolean throwsNothingOrExceptionOrThrowable(@Nullable List th) (TypeUtils.isOfClassType(th.get(0).getType(), FQN_JAVA_LANG_EXCEPTION) || TypeUtils.isOfClassType(th.get(0).getType(), "java.lang.Throwable")); } - - private boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) { - for (J.Annotation a : method.getLeadingAnnotations()) { - if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) { - return true; - } - } - return false; - } }); } + + private static boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) { + for (J.Annotation a : method.getLeadingAnnotations()) { + for (AnnotationMatcher matcher : JUNIT5_TEST_ANNOTATIONS) { + if (matcher.matches(a)) { + return true; + } + } + } + return false; + } } diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java b/src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java index aef6dbfae..c105185af 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java @@ -22,6 +22,7 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; +import org.openrewrite.java.AnnotationMatcher; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.UsesType; @@ -33,13 +34,20 @@ import org.openrewrite.java.tree.TypeUtils; import org.openrewrite.staticanalysis.java.JavaFileChecker; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import static java.util.Arrays.asList; import static java.util.Objects.requireNonNull; public class TestMethodsShouldBeVoid extends Recipe { private static final String TEST_ANNOTATION_TYPE_PATTERN = "org..*Test"; + private static final List JUNIT5_TEST_ANNOTATIONS = asList( + new AnnotationMatcher("@org.junit.jupiter.api.Test"), + new AnnotationMatcher("@org.junit.jupiter.api.RepeatedTest"), + new AnnotationMatcher("@org.junit.jupiter.params.ParameterizedTest"), + new AnnotationMatcher("@org.junit.jupiter.api.TestTemplate")); @Getter final String displayName = "Test methods should have void return type"; @@ -157,11 +165,10 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Atomi private static boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) { for (J.Annotation a : method.getLeadingAnnotations()) { - if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") || - TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")) { - return true; + for (AnnotationMatcher matcher : JUNIT5_TEST_ANNOTATIONS) { + if (matcher.matches(a)) { + return true; + } } } return false; diff --git a/src/main/java/org/openrewrite/java/testing/easymock/EasyMockVerifyToMockitoVerify.java b/src/main/java/org/openrewrite/java/testing/easymock/EasyMockVerifyToMockitoVerify.java index 88e450480..ea6b56fdd 100644 --- a/src/main/java/org/openrewrite/java/testing/easymock/EasyMockVerifyToMockitoVerify.java +++ b/src/main/java/org/openrewrite/java/testing/easymock/EasyMockVerifyToMockitoVerify.java @@ -24,6 +24,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.SemanticallyEqual; import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; @@ -118,7 +119,7 @@ private boolean isExpectInvocation(J.MethodInvocation mi, J.Identifier dependenc mi.getArguments().size() == 1 && mi.getArguments().get(0) instanceof J.MethodInvocation && ((J.MethodInvocation) mi.getArguments().get(0)).getSelect() instanceof J.Identifier && - dependency.getSimpleName().equals(((J.Identifier) ((J.MethodInvocation) mi.getArguments().get(0)).getSelect()).getSimpleName()); + SemanticallyEqual.areEqual(dependency, ((J.MethodInvocation) mi.getArguments().get(0)).getSelect()); } // match: expect(.someMethod()).andReturn(); diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java index aa993a51f..b6951c9a4 100644 --- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java @@ -16,6 +16,7 @@ package org.openrewrite.java.testing.hamcrest; import lombok.Getter; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -83,6 +84,7 @@ enum Replacement { final String hamcrest, junitPositive, junitNegative, template, kotlinTemplate; final String argumentsMethod; + final MethodMatcher matcher; Replacement(String hamcrest, String junitPositive, String junitNegative, String template, String argumentsMethod) { this(hamcrest, junitPositive, junitNegative, template, template, argumentsMethod); @@ -95,6 +97,16 @@ enum Replacement { this.template = template; this.kotlinTemplate = kotlinTemplate; this.argumentsMethod = argumentsMethod; + this.matcher = new MethodMatcher("org.hamcrest..* " + hamcrest + "(..)"); + } + + static @Nullable Replacement of(J.MethodInvocation matcherInvocation) { + for (Replacement replacement : values()) { + if (replacement.matcher.matches(matcherInvocation)) { + return replacement; + } + } + return null; } private static final Map>> methods = new HashMap<>(); @@ -167,10 +179,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } } - Replacement replacement; - try { - replacement = Replacement.valueOf(matcherInvocation.getSimpleName().toUpperCase()); - } catch (IllegalArgumentException e) { + Replacement replacement = Replacement.of(matcherInvocation); + if (replacement == null) { return mi; } boolean kotlin = getCursor().firstEnclosing(K.CompilationUnit.class) != null; diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java index c810ce751..60a8a1c8e 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java @@ -275,6 +275,8 @@ private static boolean isJmockitArgumentMatcher(Expression expression) { return false; } J.Identifier identifier = (J.Identifier) expression; - return JMOCKIT_ARGUMENT_MATCHERS.contains(identifier.getSimpleName()); + return JMOCKIT_ARGUMENT_MATCHERS.contains(identifier.getSimpleName()) && + identifier.getFieldType() != null && + TypeUtils.isOfClassType(identifier.getFieldType().getOwner(), "mockit.Invocations"); } } diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 615b35e98..22ea805b2 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -25,6 +25,8 @@ import org.openrewrite.Tree; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.SemanticallyEqual; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -38,6 +40,7 @@ class JMockitBlockRewriter { + private static final MethodMatcher RETURNS_MATCHER = new MethodMatcher("mockit.Expectations returns(..)"); private static final String WHEN_TEMPLATE_PREFIX = "when(#{any()})."; private static final String VERIFY_NO_INTERACTIONS_TEMPLATE_PREFIX = "verifyNoMoreInteractions("; private static final String VERIFY_IN_ORDER_TEMPLATE_PREFIX_1 = "InOrder inOrder"; @@ -124,12 +127,12 @@ J.Block rewriteMethodBody() { if (select instanceof J.Identifier) { J.Identifier mockObj = (J.Identifier) select; // ensure it's not a returns statement, we add that later to related statements - if (!"returns".equals(invocation.getName().getSimpleName())) { + if (!RETURNS_MATCHER.matches(invocation)) { methodInvocationIdx++; methodInvocationsToRewrite.add(new ArrayList<>()); } if ((isFullVerifications() || isVerificationsInOrder()) && - uniqueMocks.stream().noneMatch(mock -> mock.getSimpleName().equals(mockObj.getSimpleName()))) { + uniqueMocks.stream().noneMatch(mock -> SemanticallyEqual.areEqual(mock, mockObj))) { uniqueMocks.add(mockObj); } } diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java index ad9046f00..d116ba094 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java @@ -19,6 +19,7 @@ import lombok.AllArgsConstructor; import org.openrewrite.ExecutionContext; import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.search.SemanticallyEqual; import org.openrewrite.java.tree.*; import java.util.ArrayList; @@ -42,10 +43,10 @@ J.Block rewriteMethodBody() { continue; } J.NewClass nc = (J.NewClass) s; - Set spies = new HashSet<>(); + List spies = new ArrayList<>(); for (Expression newClassArg : nc.getArguments()) { if (newClassArg instanceof J.Identifier) { - spies.add(((J.Identifier) newClassArg).getSimpleName()); + spies.add((J.Identifier) newClassArg); } } @@ -139,7 +140,7 @@ private void rewriteBodyStatement(Statement statement, JavaCoordinates coordinat methodBody = methodBody.withStatements(statements); } - private boolean isSetupStatement(Statement expectationStatement, Set spies) { + private boolean isSetupStatement(Statement expectationStatement, List spies) { if (expectationStatement instanceof J.MethodInvocation) { // a method invocation on a mock is not a setup statement J.MethodInvocation methodInvocation = (J.MethodInvocation) expectationStatement; @@ -179,8 +180,8 @@ private static JavaType getVariableTypeFromAssignment(J.Assignment assignment) { return identifier.getFieldType() != null ? identifier.getFieldType().getOwner() : identifier.getType(); } - private static boolean isNotMockIdentifier(J.Identifier identifier, Set spies) { - if (spies.contains(identifier.getSimpleName())) { + private static boolean isNotMockIdentifier(J.Identifier identifier, List spies) { + if (spies.stream().anyMatch(spy -> SemanticallyEqual.areEqual(spy, identifier))) { return false; } if (identifier.getType() instanceof JavaType.Method && diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java b/src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java index 0a4f024e5..345ae7125 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java @@ -24,6 +24,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.java.ChangeMethodTargetToStatic; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; @@ -47,6 +48,10 @@ public TreeVisitor getVisitor() { public static class AssertToAssertionsVisitor extends JavaIsoVisitor { private static final JavaType ASSERTION_TYPE = JavaType.buildType("org.junit.Assert"); + private static final MethodMatcher ASSERT_MATCHER = new MethodMatcher("org.junit.Assert *(..)"); + private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.junit.Assert assertThat(..)"); + private static final MethodMatcher ASSERT_SAME_OR_NOT_SAME_MATCHER = new MethodMatcher("org.junit.Assert assert*Same(*, *)"); + private static final MethodMatcher ASSERT_EQUALS_OR_NOT_EQUALS_MATCHER = new MethodMatcher("org.junit.Assert assert*Equals(*, *)"); @Override public @Nullable J preVisit(J tree, ExecutionContext ctx) { @@ -90,13 +95,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu List> args = m.getPadding().getArguments().getPadding().getElements(); Expression firstArg = args.get(0).getElement(); // 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())) { - return m; - } + if (ASSERT_SAME_OR_NOT_SAME_MATCHER.matches(m) || ASSERT_EQUALS_OR_NOT_EQUALS_MATCHER.matches(m)) { + return m; } if (TypeUtils.isString(firstArg.getType())) { // Move the first arg to be the last argument @@ -149,15 +149,8 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Execu return mr; } - private static boolean isJunitAssertMethod(MethodCall memberRef) { - if (memberRef.getMethodType() == null || !TypeUtils.isOfType(ASSERTION_TYPE, memberRef.getMethodType().getDeclaringType())) { - return false; - } - if (memberRef instanceof J.MemberReference) { - return !"assertThat".equals(((J.MemberReference) memberRef).getReference().getSimpleName()); - } - return memberRef instanceof J.MethodInvocation && - !"assertThat".equals(((J.MethodInvocation) memberRef).getSimpleName()); + private static boolean isJunitAssertMethod(MethodCall call) { + return ASSERT_MATCHER.matches(call) && !ASSERT_THAT_MATCHER.matches(call); } } } diff --git a/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java b/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java index bd1d84c1d..558bb9a5c 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java @@ -45,6 +45,10 @@ public class MigrateJUnitTestCase extends Recipe { private static final AnnotationMatcher JUNIT_AFTER_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*After*"); private static final AnnotationMatcher JUNIT_BEFORE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*Before*"); private static final MethodMatcher TEST_CASE_RUN_MATCHER = new MethodMatcher("junit.framework.TestCase run(junit.framework.TestResult)", true); + private static final MethodMatcher SET_NAME_MATCHER = new MethodMatcher("junit.framework.TestCase setName(String)"); + private static final MethodMatcher SET_UP_MATCHER = new MethodMatcher("junit.framework.TestCase setUp()"); + private static final MethodMatcher TEAR_DOWN_MATCHER = new MethodMatcher("junit.framework.TestCase tearDown()"); + private static final MethodMatcher SUITE_MATCHER = new MethodMatcher("*..* suite()"); private static boolean isSupertypeTestCase(JavaType.@Nullable FullyQualified fullyQualified) { if (fullyQualified == null || fullyQualified.getSupertype() == null || "java.lang.Object".equals(fullyQualified.getFullyQualifiedName())) { @@ -97,8 +101,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { Space end = b.getEnd(); for (int i = statements.size() - 1; i >= 0; i--) { Statement statement = statements.get(i); - if (statement instanceof J.MethodInvocation && isTestCaseMethod((J.MethodInvocation) statement) && - "setName".equals(((J.MethodInvocation) statement).getSimpleName())) { + if (statement instanceof J.MethodInvocation && SET_NAME_MATCHER.matches((J.MethodInvocation) statement)) { statements.remove(i); if (i < statements.size()) { statements.set(i, statements.get(i).withPrefix(commentOut(statement, statements.get(i).getPrefix()))); @@ -122,7 +125,7 @@ private Space commentOut(Statement statement, Space next) { public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); // setUp and tearDown will be invoked via Before and After annotations - if (isTestCaseMethod(mi) && ("setUp".equals(mi.getSimpleName()) || "tearDown".equals(mi.getSimpleName()))) { + if (SET_UP_MATCHER.matches(mi) || TEAR_DOWN_MATCHER.matches(mi)) { return null; } return mi; @@ -130,12 +133,6 @@ private Space commentOut(Statement statement, Space next) { }); } - @SuppressWarnings("ConstantConditions") - private static boolean isTestCaseMethod(J.MethodInvocation mi) { - return (mi.getSelect() != null && TypeUtils.isOfClassType(mi.getSelect().getType(), "junit.framework.TestCase")) || - (mi.getMethodType() != null && TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), "junit.framework.TestCase")); - } - private static class TestCaseVisitor extends JavaIsoVisitor { private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("@java.lang.Override"); private static final MethodMatcher TEST_CASE_SUPER_MATCHER = new MethodMatcher("junit.framework.TestCase (..)"); @@ -171,9 +168,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex } // Remove suite() methods that return junit.framework.Test or TestSuite - if ("suite".equals(md.getSimpleName()) && - md.hasModifier(J.Modifier.Type.Static) && - md.getMethodType() != null && + if (md.hasModifier(J.Modifier.Type.Static) && + SUITE_MATCHER.matches(md.getMethodType()) && (TypeUtils.isOfClassType(md.getMethodType().getReturnType(), "junit.framework.Test") || TypeUtils.isOfClassType(md.getMethodType().getReturnType(), "junit.framework.TestSuite"))) { maybeRemoveImport("junit.framework.Test"); diff --git a/src/main/java/org/openrewrite/java/testing/mockito/MockitoUtils.java b/src/main/java/org/openrewrite/java/testing/mockito/MockitoUtils.java index 5ad66c550..0cfdcb70b 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/MockitoUtils.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/MockitoUtils.java @@ -33,10 +33,16 @@ import java.util.Set; import java.util.UUID; +import static java.util.Arrays.asList; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; public class MockitoUtils { + private static final List TEST_ANNOTATION_MATCHERS = asList( + new AnnotationMatcher("@org.junit.Test"), + new AnnotationMatcher("@org.junit.jupiter.api.Test"), + new AnnotationMatcher("@org.testng.annotations.Test")); + public static J.ClassDeclaration maybeAddMethodWithAnnotation( JavaVisitor visitor, J.ClassDeclaration classDecl, @@ -123,8 +129,10 @@ private static boolean hasMethodWithAnnotation(J.ClassDeclaration classDecl, Ann private static J.@Nullable MethodDeclaration getFirstTestMethod(List methods) { for (J.MethodDeclaration methodDeclaration : methods) { for (J.Annotation annotation : methodDeclaration.getLeadingAnnotations()) { - if ("Test".equals(annotation.getSimpleName())) { - return methodDeclaration; + for (AnnotationMatcher matcher : TEST_ANNOTATION_MATCHERS) { + if (matcher.matches(annotation)) { + return methodDeclaration; + } } } } diff --git a/src/main/java/org/openrewrite/java/testing/mockito/WhiteboxToReflectionVisitor.java b/src/main/java/org/openrewrite/java/testing/mockito/WhiteboxToReflectionVisitor.java index 5d4f37d45..9ab0d3f00 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/WhiteboxToReflectionVisitor.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/WhiteboxToReflectionVisitor.java @@ -800,6 +800,8 @@ boolean hasArrayArg(List args, int fromIndex) { new AnnotationMatcher("@org.testng.annotations.BeforeClass"), new AnnotationMatcher("@org.testng.annotations.AfterClass")); + private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("@java.lang.Override"); + private static boolean isTestOrFixtureMethod(J.MethodDeclaration md) { for (J.Annotation annotation : md.getLeadingAnnotations()) { for (AnnotationMatcher matcher : TEST_OR_FIXTURE) { @@ -822,7 +824,7 @@ private static boolean overridesASupertypeMethod(J.MethodDeclaration md) { return true; // unknown type: assume the signature is load-bearing } for (J.Annotation annotation : md.getLeadingAnnotations()) { - if ("Override".equals(annotation.getSimpleName())) { + if (OVERRIDE_ANNOTATION_MATCHER.matches(annotation)) { return true; } }