From cd282ee0aef27b4fa1ca6a89b610f2a66509a0c0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 24 Oct 2026 14:48:38 +0200 Subject: [PATCH] Only remove a direct `PowerMockTestCase` or `PowerMockConfiguration` superclass in `RemovePowerMockClassExtensions` The recipe checked the `extends` clause with `TypeUtils.isAssignableTo`, so a test such as `class FooTest extends MyBaseTest`, where the user's own `MyBaseTest` extends `PowerMockTestCase`, also lost its superclass and with it every inherited helper, leaving code that no longer compiles. Matching the exact type with `TypeUtils.isOfClassType` unwraps only the base class that extends PowerMock directly and keeps subclasses of it intact. --- .../RemovePowerMockClassExtensions.java | 2 +- .../RemovePowerMockClassExtensionsTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java b/src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java index 92cbb4adf..b3dcddbca 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java @@ -55,7 +55,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex private J.ClassDeclaration maybeRemoveExtension(J.ClassDeclaration classDecl, String extensionFQN) { TypeTree extension = classDecl.getExtends(); - if (extension != null && TypeUtils.isAssignableTo(extensionFQN, extension.getType())) { + if (extension != null && TypeUtils.isOfClassType(extension.getType(), extensionFQN)) { classDecl = classDecl.withExtends(null); maybeRemoveImport(extensionFQN); } diff --git a/src/test/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensionsTest.java b/src/test/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensionsTest.java index 7ccea2b6a..23ec728db 100644 --- a/src/test/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensionsTest.java +++ b/src/test/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensionsTest.java @@ -81,4 +81,45 @@ public class MyPowerMockConfiguration {} """ )); } + + @Test + void subclassOfOwnBaseClassKeepsExtension() { + //language=java + rewriteRun( + java( + """ + package org.powermock.modules.testng; + + public class PowerMockTestCase {} + """ + ), + java( + """ + import org.powermock.modules.testng.PowerMockTestCase; + + abstract class MyBaseTest extends PowerMockTestCase { + protected int helper() { + return 1; + } + } + """, + """ + abstract class MyBaseTest { + protected int helper() { + return 1; + } + } + """ + ), + java( + """ + class FooTest extends MyBaseTest { + void test() { + helper(); + } + } + """ + ) + ); + } }