Skip to content

Commit af19fad

Browse files
authored
Detect JUnit version in PowerMockitoMockStaticToMockito (#919)
* Add failing test for JUnit 4 PowerMockito migration Reproduces moderneinc/customer-requests#1925: when a test class uses JUnit 4 (@org.junit.Test), ReplacePowerMockito should use @Before/@after instead of @BeforeEach/@AfterEach. * Detect JUnit version in PowerMockitoMockStaticToMockito When a test class uses JUnit 4 (@org.junit.Test), the recipe now uses @Before/@after from org.junit instead of always defaulting to @BeforeEach/@AfterEach from JUnit Jupiter. Fixes moderneinc/customer-requests#1925
1 parent c2860b4 commit af19fad

2 files changed

Lines changed: 74 additions & 15 deletions

File tree

src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private static class PowerMockitoToMockitoVisitor extends JavaVisitor<ExecutionC
8888
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
8989
if (tree instanceof JavaSourceFile) {
9090
boolean useTestNg = !FindAnnotations.find((J) tree, "@org.testng.annotations.Test").isEmpty();
91-
initTestFrameworkInfo(useTestNg);
91+
boolean useJunit4 = !useTestNg && !FindAnnotations.find((J) tree, "@org.junit.Test").isEmpty();
92+
initTestFrameworkInfo(useTestNg, useJunit4);
9293
}
9394
return super.visit(tree, ctx);
9495
}
@@ -358,22 +359,29 @@ private J.ClassDeclaration removeExtension(J.ClassDeclaration classDecl, String
358359
return classDecl;
359360
}
360361

361-
private void initTestFrameworkInfo(boolean useTestNg) {
362+
private void initTestFrameworkInfo(boolean useTestNg, boolean useJunit4) {
362363
String setUpMethodAnnotationName;
363364
String tearDownMethodAnnotationName;
364365
String annotationPackage;
365366

366-
if (!useTestNg) {
367-
setUpMethodAnnotationName = "BeforeEach";
368-
tearDownMethodAnnotationName = "AfterEach";
369-
annotationPackage = "org.junit.jupiter.api";
370-
additionalClasspathResource = "junit-jupiter-api-5";
371-
} else {
367+
if (useTestNg) {
372368
setUpMethodAnnotationName = "BeforeMethod";
373369
tearDownMethodAnnotationName = "AfterMethod";
374370
annotationPackage = "org.testng.annotations";
375371
additionalClasspathResource = "testng-7";
376372
tearDownMethodAnnotationParameters = "(alwaysRun = true)";
373+
} else if (useJunit4) {
374+
setUpMethodAnnotationName = "Before";
375+
tearDownMethodAnnotationName = "After";
376+
annotationPackage = "org.junit";
377+
additionalClasspathResource = "junit-4";
378+
tearDownMethodAnnotationParameters = "";
379+
} else {
380+
setUpMethodAnnotationName = "BeforeEach";
381+
tearDownMethodAnnotationName = "AfterEach";
382+
annotationPackage = "org.junit.jupiter.api";
383+
additionalClasspathResource = "junit-jupiter-api-5";
384+
tearDownMethodAnnotationParameters = "";
377385
}
378386

379387
this.setUpMethodAnnotation = "@" + setUpMethodAnnotationName;

src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,9 @@ public void testStaticMethod() {
654654
""",
655655

656656
"""
657+
import org.junit.After;
657658
import org.junit.Before;
658659
import org.junit.Test;
659-
import org.junit.jupiter.api.AfterEach;
660-
import org.junit.jupiter.api.BeforeEach;
661660
import org.mockito.MockedStatic;
662661
import org.mockito.Mockito;
663662
import test.A;
@@ -670,14 +669,10 @@ public class MyTest {
670669
671670
@Before
672671
void setUp() {
673-
}
674-
675-
@BeforeEach
676-
void setUpStaticMocks() {
677672
mockedA_B = Mockito.mockStatic(A.B.class);
678673
}
679674
680-
@AfterEach
675+
@After
681676
void tearDownStaticMocks() {
682677
mockedA_B.closeOnDemand();
683678
}
@@ -772,4 +767,60 @@ fun testStaticMethod() {
772767
)
773768
);
774769
}
770+
771+
@Issue("https://github.com/moderneinc/customer-requests/issues/1925")
772+
@Test
773+
void junit4PrepareForTestUsesBeforeAndAfter() {
774+
//language=java
775+
rewriteRun(
776+
java(
777+
"""
778+
import static org.mockito.Mockito.mockStatic;
779+
780+
import java.util.Calendar;
781+
782+
import org.junit.Test;
783+
import org.powermock.core.classloader.annotations.PrepareForTest;
784+
785+
@PrepareForTest({Calendar.class})
786+
public class MyTest {
787+
788+
@Test
789+
public void testStaticMethod() {
790+
mockStatic(Calendar.class);
791+
}
792+
}
793+
""",
794+
"""
795+
import static org.mockito.Mockito.mockStatic;
796+
797+
import java.util.Calendar;
798+
799+
import org.junit.After;
800+
import org.junit.Before;
801+
import org.junit.Test;
802+
import org.mockito.MockedStatic;
803+
804+
public class MyTest {
805+
806+
private MockedStatic<Calendar> mockedCalendar;
807+
808+
@Before
809+
void setUpStaticMocks() {
810+
mockedCalendar = mockStatic(Calendar.class);
811+
}
812+
813+
@After
814+
void tearDownStaticMocks() {
815+
mockedCalendar.closeOnDemand();
816+
}
817+
818+
@Test
819+
public void testStaticMethod() {
820+
}
821+
}
822+
"""
823+
)
824+
);
825+
}
775826
}

0 commit comments

Comments
 (0)