From ebdbf2356d97ba6291eedae620efe4a750f99987 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Thu, 20 Jun 2024 12:30:32 +1000 Subject: [PATCH] Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue --- .../jmockit/ExpectationsBlockRewriter.java | 19 ++- .../JMockitExpectationsToMockitoTest.java | 143 ++++++++++++++++++ 2 files changed, 155 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java index a1522518d..ae2dce1ee 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java @@ -17,12 +17,12 @@ import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.tree.*; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; @@ -128,15 +128,20 @@ private void rewriteMethodBody(List expectationStatements) { removeExpectationsStatement(); } - // TODO doesn't cover cases with multiple times eg minTimes and maxTimes - this is typically rare scenario + boolean hasTimes = false; if (mockInvocationResults.getTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getTimes(), "times"); - } else if (mockInvocationResults.getMinTimes() != null) { + } + if (mockInvocationResults.getMinTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); - } else if (mockInvocationResults.getMaxTimes() != null) { + } + if (mockInvocationResults.getMaxTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); - } else if (!hasExpectationsResults) { - // no times, no results + } + if (!hasExpectationsResults && !hasTimes) { writeMethodVerification(invocation, null, null); } } @@ -263,7 +268,7 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil private static String getVerifyTemplate(List arguments, String fqn, @Nullable String verificationMode, List templateParams) { StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}"); // verify(object if (verificationMode != null) { - templateBuilder.append(", " + verificationMode + "(#{any(int)})"); // verify(object, times(2) + templateBuilder.append(", ").append(verificationMode).append("(#{any(int)})"); // verify(object, times(2) } templateBuilder.append(").#{}("); // verify(object, times(2)).method( diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java index e8f8fa1eb..69f4627fe 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -967,6 +967,149 @@ void test() { ); } + @Test + void whenMinTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + minTimes = 2; + }}; + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject, atLeast(2)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMaxTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + maxTimes = 5; + }}; + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject, atMost(5)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMinTimesMaxTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + minTimes = 1; + maxTimes = 3; + }}; + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject, atLeast(1)).wait(anyLong(), anyInt()); + verify(myObject, atMost(3)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + @Test void whenSpy() { //language=java