diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java index c8dfe2387..0a6b4ebb3 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java @@ -18,7 +18,6 @@ import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; -import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Option; @@ -154,9 +153,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex return m; } - if (m.getModifiers().stream().anyMatch(mod -> (mod.getType() == J.Modifier.Type.Public || (orProtected && mod.getType() == J.Modifier.Type.Protected))) - && Boolean.FALSE.equals(TypeUtils.isOverride(method.getMethodType())) - && hasJUnit5MethodAnnotation(m)) { + if (m.hasModifier(J.Modifier.Type.Abstract) || TypeUtils.isOverride(method.getMethodType())) { + return m; + } + + if ((m.hasModifier(J.Modifier.Type.Public) || (orProtected && m.hasModifier(J.Modifier.Type.Protected))) && + hasJUnit5MethodAnnotation(m)) { // remove public modifier doAfterVisit(new ChangeMethodAccessLevelVisitor<>(new MethodMatcher(method), null)); } diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublicTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublicTest.java index 3d9679549..6e682caac 100644 --- a/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublicTest.java +++ b/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublicTest.java @@ -53,7 +53,7 @@ void testMethod() { @Nested public class NestedTestClass { - + @Test void anotherTestMethod() { } @@ -198,9 +198,9 @@ void ignoreOverriddenMethod() { abstract class AbstractTest { public abstract void testMethod(); } - + class BTest extends AbstractTest { - + @Test @Override public void testMethod() {