Skip to content

Commit a72d7b1

Browse files
authored
Fix Mockito1to3Migration not adding runner after removing PowerMockRunner (#942)
ReplacePowerMockito was ordered after AddMockitoExtensionIfAnnotationsUsed, so the latter would see @RunWith(PowerMockRunner.class) and skip the class. Then ReplacePowerMockito would remove it, leaving @mock fields uninitialized. Move ReplacePowerMockito before AddMockitoExtensionIfAnnotationsUsed so the runner gap is detected and filled with MockitoJUnitRunner or MockitoExtension.
1 parent b15c883 commit a72d7b1

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/main/resources/META-INF/rewrite/mockito.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ recipeList:
179179
newFullyQualifiedTypeName: org.mockito.junit.MockitoJUnitRunner
180180
- org.openrewrite.java.testing.mockito.CleanupMockitoImports
181181
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
182+
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
182183
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
183184
- org.openrewrite.java.testing.mockito.AddMockitoExtensionIfAnnotationsUsed
184185
- org.openrewrite.java.testing.mockito.AddMockitoJupiterDependency
185186
- org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJupiter
186187
- org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJUnit4
187188
- org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForTestNG
188189
- org.openrewrite.java.testing.mockito.RemoveInitMocksIfRunnersSpecified
189-
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
190190
- org.openrewrite.java.dependencies.ChangeDependency:
191191
oldGroupId: org.mockito
192192
oldArtifactId: mockito-all

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,56 @@ void someTest() {
406406
);
407407
}
408408

409+
@Test
410+
void powerMockRunnerReplacedWithMockitoJUnitRunnerWhenMockAnnotationsPresent() {
411+
rewriteRun(
412+
spec -> spec.parser(JavaParser.fromJavaVersion()
413+
.logCompilationWarningsAndErrors(true)
414+
.classpathFromResources(new InMemoryExecutionContext(),
415+
"mockito-core-3.12",
416+
"junit-4",
417+
"powermock-core-1",
418+
"powermock-api-mockito-1",
419+
"powermock-api-support-1",
420+
"powermock-module-junit4")),
421+
//language=java
422+
java(
423+
"""
424+
import org.junit.Test;
425+
import org.junit.runner.RunWith;
426+
import org.mockito.Mock;
427+
import org.powermock.modules.junit4.PowerMockRunner;
428+
429+
@RunWith(PowerMockRunner.class)
430+
public class MyTest {
431+
@Mock
432+
Object myMock;
433+
434+
@Test
435+
public void someTest() {
436+
}
437+
}
438+
""",
439+
"""
440+
import org.junit.Test;
441+
import org.junit.runner.RunWith;
442+
import org.mockito.Mock;
443+
import org.mockito.junit.MockitoJUnitRunner;
444+
445+
@RunWith(MockitoJUnitRunner.class)
446+
public class MyTest {
447+
@Mock
448+
Object myMock;
449+
450+
@Test
451+
public void someTest() {
452+
}
453+
}
454+
"""
455+
)
456+
);
457+
}
458+
409459
@Test
410460
void handlesAnyObjectFromMockitoWildCardImport() {
411461
rewriteRun(

0 commit comments

Comments
 (0)