Skip to content

Commit 2621b64

Browse files
Laurens-Wtimtebeek
andauthored
PowerMockitoMockStaticToMockito duplicates variables and methods when static nested class is involved (#588)
* Setup testcase that shows the issue * This will fix it for now but we may need a more structural solution to sanitize variable names * Apply suggestions from code review --------- Co-authored-by: Laurens Westerlaken <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent b64182b commit 2621b64

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public String getDescription() {
4343
public TreeVisitor<?, ExecutionContext> getVisitor() {
4444
return Preconditions.check(
4545
Preconditions.or(
46-
new UsesType<>("org.powermock..*", false),
47-
new UsesType<>("org.mockito..*", false)
46+
new UsesType<>("org.powermock..*", false),
47+
new UsesType<>("org.mockito..*", false)
4848
),
4949
new PowerMockitoToMockitoVisitor()
5050
);
@@ -446,7 +446,7 @@ private J.ClassDeclaration addFieldDeclarationForMockedTypes(J.ClassDeclaration
446446
new Cursor(getCursor().getParentOrThrow(), classDecl),
447447
classDecl.getBody().getCoordinates().firstStatement(),
448448
classlessTypeName,
449-
classlessTypeName
449+
classlessTypeName.replace(".", "_")
450450
);
451451

452452
J.VariableDeclarations mockField = (J.VariableDeclarations) classDecl.getBody().getStatements().get(0);

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,85 @@ void testTryWithResource() {
602602
);
603603
}
604604

605+
@Test
606+
void shouldNotDuplicateVarsAndMethods() {
607+
//language=java
608+
rewriteRun(
609+
java(
610+
"""
611+
package test;
612+
613+
public class A {
614+
public static class B {
615+
public static String helloWorld() {
616+
return "Hello World";
617+
}
618+
}
619+
}
620+
"""
621+
),
622+
java(
623+
"""
624+
import org.junit.Before;
625+
import org.junit.Test;
626+
import org.mockito.Mockito;
627+
import org.powermock.core.classloader.annotations.PrepareForTest;
628+
import test.A;
629+
630+
@PrepareForTest({ A.B.class })
631+
public class MyTest {
632+
633+
private static final String TEST_MESSAGE = "this is a test message";
634+
635+
@Before
636+
void setUp() {
637+
Mockito.mockStatic(A.B.class);
638+
}
639+
640+
@Test
641+
public void testStaticMethod() {
642+
}
643+
}
644+
""",
645+
646+
"""
647+
import org.junit.Before;
648+
import org.junit.Test;
649+
import org.junit.jupiter.api.AfterEach;
650+
import org.junit.jupiter.api.BeforeEach;
651+
import org.mockito.MockedStatic;
652+
import org.mockito.Mockito;
653+
import test.A;
654+
655+
public class MyTest {
656+
657+
private MockedStatic<A.B> mockedA_B;
658+
659+
private static final String TEST_MESSAGE = "this is a test message";
660+
661+
@Before
662+
void setUp() {
663+
}
664+
665+
@BeforeEach
666+
void setUpStaticMocks() {
667+
mockedA_B = Mockito.mockStatic(A.B.class);
668+
}
669+
670+
@AfterEach
671+
void tearDownStaticMocks() {
672+
mockedA_B.closeOnDemand();
673+
}
674+
675+
@Test
676+
public void testStaticMethod() {
677+
}
678+
}
679+
"""
680+
)
681+
);
682+
}
683+
605684
@Test
606685
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/358")
607686
void doesNotExplodeOnTopLevelMethodDeclaration() {

0 commit comments

Comments
 (0)