Skip to content

Commit

Permalink
PowerMockitoMockStaticToMockito duplicates variables and methods wh…
Browse files Browse the repository at this point in the history
…en 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]>
  • Loading branch information
3 people authored Sep 2, 2024
1 parent b64182b commit 2621b64
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.or(
new UsesType<>("org.powermock..*", false),
new UsesType<>("org.mockito..*", false)
new UsesType<>("org.powermock..*", false),
new UsesType<>("org.mockito..*", false)
),
new PowerMockitoToMockitoVisitor()
);
Expand Down Expand Up @@ -446,7 +446,7 @@ private J.ClassDeclaration addFieldDeclarationForMockedTypes(J.ClassDeclaration
new Cursor(getCursor().getParentOrThrow(), classDecl),
classDecl.getBody().getCoordinates().firstStatement(),
classlessTypeName,
classlessTypeName
classlessTypeName.replace(".", "_")
);

J.VariableDeclarations mockField = (J.VariableDeclarations) classDecl.getBody().getStatements().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,85 @@ void testTryWithResource() {
);
}

@Test
void shouldNotDuplicateVarsAndMethods() {
//language=java
rewriteRun(
java(
"""
package test;
public class A {
public static class B {
public static String helloWorld() {
return "Hello World";
}
}
}
"""
),
java(
"""
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import test.A;
@PrepareForTest({ A.B.class })
public class MyTest {
private static final String TEST_MESSAGE = "this is a test message";
@Before
void setUp() {
Mockito.mockStatic(A.B.class);
}
@Test
public void testStaticMethod() {
}
}
""",

"""
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import test.A;
public class MyTest {
private MockedStatic<A.B> mockedA_B;
private static final String TEST_MESSAGE = "this is a test message";
@Before
void setUp() {
}
@BeforeEach
void setUpStaticMocks() {
mockedA_B = Mockito.mockStatic(A.B.class);
}
@AfterEach
void tearDownStaticMocks() {
mockedA_B.closeOnDemand();
}
@Test
public void testStaticMethod() {
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/358")
void doesNotExplodeOnTopLevelMethodDeclaration() {
Expand Down

0 comments on commit 2621b64

Please sign in to comment.