Skip to content

Commit da6a123

Browse files
Don't remove mockStatic() calls in resources of try-with-resources (#426)
* Don't remove `mockStatic()` calls in resources of try-with-resources This commit does not yet close the issue, as there are other cases, where this can potentially go wrong. It also appears strange that the recipe matches on Mockito's `mockStatic()` rather than on PowerMock's `mockStatic()`. Therefore, the recipe is for now excluded from `Mockito1to3Migration`, as this is part of `JUnit4to5Migration` which is widely used. Issue: #360 * Update src/main/resources/META-INF/rewrite/mockito.yml Co-authored-by: Tim te Beek <[email protected]> --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 60ef401 commit da6a123

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
189189

190190
if (MOCKED_STATIC_MATCHER.matches(mi)) {
191191
determineTestGroups();
192-
if (getCursor().firstEnclosing(J.Assignment.class) == null) {
192+
if (!getCursor().getPath(o -> o instanceof J.Assignment || o instanceof J.Try.Resource).hasNext()) {
193193
//noinspection DataFlowIssue
194194
return null;
195195
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ recipeList:
111111
- org.openrewrite.java.testing.mockito.CleanupMockitoImports
112112
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
113113
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
114-
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
114+
# https://github.com/openrewrite/rewrite-testing-frameworks/issues/360
115+
# - org.openrewrite.java.testing.mockito.ReplacePowerMockito
115116
- org.openrewrite.java.dependencies.AddDependency:
116117
groupId: org.mockito
117118
artifactId: mockito-junit-jupiter

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ void testSomething() { }
256256
)
257257
);
258258
}
259+
259260
@Test
260261
void tearDownMethodOfTestNGWithAnnotationRemainsUntouched() {
261-
//language=java
262+
//language=java
262263
rewriteRun(
263264
java(
264265
"""
@@ -303,6 +304,7 @@ void testSomething() { }
303304
)
304305
);
305306
}
307+
306308
@Test
307309
void tearDownMethodOfTestNGHasAnnotationWithSameArgumentsAsTheTestThatCallsMockStatic() {
308310
//language=java
@@ -539,7 +541,7 @@ void extensionOfPowerMockTestCaseGetsRemoved() {
539541
public class PowerMockTestCase {}
540542
"""
541543
),
542-
java(
544+
java(
543545
"""
544546
import org.powermock.modules.testng.PowerMockTestCase;
545547
@@ -548,7 +550,7 @@ public class MyPowerMockTestCase extends PowerMockTestCase {}
548550
"""
549551
public class MyPowerMockTestCase {}
550552
""")
551-
);
553+
);
552554
}
553555

554556
@Test
@@ -562,7 +564,7 @@ void extensionOfPowerMockConfigurationGetsRemoved() {
562564
public class PowerMockConfiguration {}
563565
"""
564566
),
565-
java(
567+
java(
566568
"""
567569
import org.powermock.configuration.PowerMockConfiguration;
568570
@@ -574,12 +576,38 @@ public class MyPowerMockConfiguration {}
574576
));
575577
}
576578

579+
@Test
580+
void mockStaticInTryWithResources() {
581+
//language=java
582+
rewriteRun(
583+
java(
584+
"""
585+
import org.junit.jupiter.api.Test;
586+
import org.mockito.MockedStatic;
587+
588+
import java.nio.file.Files;
589+
590+
import static org.mockito.Mockito.mockStatic;
591+
592+
class A {
593+
@Test
594+
void testTryWithResource() {
595+
try (MockedStatic<Files> mocked = mockStatic(Files.class)) {
596+
// test logic that uses mocked
597+
}
598+
}
599+
}
600+
"""
601+
)
602+
);
603+
}
604+
577605
@Test
578606
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/358")
579607
void doesNotExplodeOnTopLevelMethodDeclaration() {
580608
rewriteRun(
581609
groovy(
582-
"def myFun() { }"
583-
));
610+
"def myFun() { }"
611+
));
584612
}
585613
}

0 commit comments

Comments
 (0)