Skip to content

Commit 300bfba

Browse files
authored
Only remove a direct PowerMockTestCase or PowerMockConfiguration superclass in RemovePowerMockClassExtensions (#1224)
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.
1 parent a07ab38 commit 300bfba

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
5555

5656
private J.ClassDeclaration maybeRemoveExtension(J.ClassDeclaration classDecl, String extensionFQN) {
5757
TypeTree extension = classDecl.getExtends();
58-
if (extension != null && TypeUtils.isAssignableTo(extensionFQN, extension.getType())) {
58+
if (extension != null && TypeUtils.isOfClassType(extension.getType(), extensionFQN)) {
5959
classDecl = classDecl.withExtends(null);
6060
maybeRemoveImport(extensionFQN);
6161
}

‎src/test/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensionsTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,45 @@ public class MyPowerMockConfiguration {}
8181
"""
8282
));
8383
}
84+
85+
@Test
86+
void subclassOfOwnBaseClassKeepsExtension() {
87+
//language=java
88+
rewriteRun(
89+
java(
90+
"""
91+
package org.powermock.modules.testng;
92+
93+
public class PowerMockTestCase {}
94+
"""
95+
),
96+
java(
97+
"""
98+
import org.powermock.modules.testng.PowerMockTestCase;
99+
100+
abstract class MyBaseTest extends PowerMockTestCase {
101+
protected int helper() {
102+
return 1;
103+
}
104+
}
105+
""",
106+
"""
107+
abstract class MyBaseTest {
108+
protected int helper() {
109+
return 1;
110+
}
111+
}
112+
"""
113+
),
114+
java(
115+
"""
116+
class FooTest extends MyBaseTest {
117+
void test() {
118+
helper();
119+
}
120+
}
121+
"""
122+
)
123+
);
124+
}
84125
}

0 commit comments

Comments
 (0)