Skip to content

Commit 9089e95

Browse files
Supporting arguments in PowerMockitoWhenNewToMockito (#686)
* Supporting arguments in PowerMockitoWhenNewToMockito * Adding test coverage for withAnyArguments()
1 parent fd5faf6 commit 9089e95

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
public class PowerMockitoWhenNewToMockito extends Recipe {
3636

3737
private static final MethodMatcher PM_WHEN_NEW = new MethodMatcher("org.powermock.api.mockito.PowerMockito whenNew(..)");
38-
private static final MethodMatcher WITH_NO_ARGUMENTS = new MethodMatcher("*..* withNoArguments(..)");
38+
private static final MethodMatcher WITH_NO_ARGUMENTS = new MethodMatcher("*..* withNoArguments()");
39+
private static final MethodMatcher WITH_ARGUMENTS = new MethodMatcher("*..* withArguments(..)");
40+
private static final MethodMatcher WITH_ANY_ARGUMENTS = new MethodMatcher("*..* withAnyArguments()");
3941
private static final MethodMatcher THEN_RETURN = new MethodMatcher("org.mockito.stubbing.OngoingStubbing thenReturn(..)");
4042
private static final MethodMatcher MOCKITO_MOCK = new MethodMatcher("org.mockito.Mockito mock(..)");
4143
private static final MethodMatcher PM_MOCK = new MethodMatcher("org.powermock.api.mockito.PowerMockito mock(..)");
@@ -57,7 +59,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5759
public @Nullable J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
5860
if (THEN_RETURN.matches(method) && method.getSelect() instanceof J.MethodInvocation) {
5961
J.MethodInvocation select1 = (J.MethodInvocation) method.getSelect();
60-
if (WITH_NO_ARGUMENTS.matches(select1) && select1.getSelect() instanceof J.MethodInvocation) {
62+
boolean withArgumentsMethodMatch = WITH_ANY_ARGUMENTS.matches(select1) || WITH_ARGUMENTS.matches(select1) || WITH_NO_ARGUMENTS.matches(select1);
63+
if (withArgumentsMethodMatch && select1.getSelect() instanceof J.MethodInvocation) {
6164
J.MethodInvocation select2 = (J.MethodInvocation) select1.getSelect();
6265
if (PM_WHEN_NEW.matches(select2) && select2.getArguments().size() == 1) {
6366
maybeRemoveImport("org.powermock.api.mockito.PowerMockito");
@@ -112,8 +115,7 @@ private JavaIsoVisitor<ExecutionContext> removeMockUsagesVisitor(List<J.FieldAcc
112115
@Override
113116
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
114117
J.VariableDeclarations ret = super.visitVariableDeclarations(multiVariable, ctx);
115-
Cursor containingMethod = getCursor().dropParentUntil(x -> x instanceof J.MethodDeclaration);
116-
if (!inMethod.equals(containingMethod.getValue())) {
118+
if (!inMethod.equals(getCursor().firstEnclosing(J.MethodDeclaration.class))) {
117119
return ret;
118120
}
119121
List<J.VariableDeclarations.NamedVariable> variables = ListUtils.filter(ret.getVariables(), varr -> {

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.openrewrite.java.testing.mockito;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
1921
import org.openrewrite.DocumentExample;
2022
import org.openrewrite.InMemoryExecutionContext;
2123
import org.openrewrite.java.JavaParser;
@@ -819,4 +821,67 @@ public final void testNumbers() throws Exception {
819821
)
820822
);
821823
}
824+
825+
@ParameterizedTest
826+
@ValueSource(strings = {"withArguments(\"Have a nice day!\")", "withAnyArguments()"})
827+
void whenNewWithArguments(String methodCall) {
828+
//language=java
829+
rewriteRun(
830+
java(
831+
"""
832+
import org.powermock.api.mockito.PowerMockito;
833+
import static org.powermock.api.mockito.PowerMockito.*;
834+
835+
import org.junit.jupiter.api.Test;
836+
import static org.junit.jupiter.api.Assertions.assertEquals;
837+
838+
public class MyTest2 {
839+
public static class SomeTexts {
840+
String text;
841+
public SomeTexts(String text) { this.text = text; }
842+
public String getText() { return text; }
843+
}
844+
845+
@Test
846+
public final void testWords() throws Exception {
847+
SomeTexts mock = PowerMockito.mock(SomeTexts.class);
848+
PowerMockito.whenNew(SomeTexts.class).METHODCALL.thenReturn(mock);
849+
850+
SomeTexts st = new SomeTexts("Have a nice day!");
851+
when(st.getText()).thenReturn("overridden");
852+
853+
assertEquals("overridden", st.getText());
854+
}
855+
}
856+
""".replaceAll("METHODCALL", methodCall),
857+
"""
858+
import org.mockito.MockedConstruction;
859+
import org.mockito.Mockito;
860+
import static org.mockito.Mockito.when;
861+
862+
import org.junit.jupiter.api.Test;
863+
import static org.junit.jupiter.api.Assertions.assertEquals;
864+
865+
public class MyTest2 {
866+
public static class SomeTexts {
867+
String text;
868+
public SomeTexts(String text) { this.text = text; }
869+
public String getText() { return text; }
870+
}
871+
872+
@Test
873+
public final void testWords() throws Exception {
874+
try (MockedConstruction<SomeTexts> mockSomeTexts = Mockito.mockConstruction(SomeTexts.class)) {
875+
876+
SomeTexts st = new SomeTexts("Have a nice day!");
877+
when(st.getText()).thenReturn("overridden");
878+
879+
assertEquals("overridden", st.getText());
880+
}
881+
}
882+
}
883+
"""
884+
)
885+
);
886+
}
822887
}

0 commit comments

Comments
 (0)