From 3fda82d6cdd2eeabde9578254d6ff8f885ea2345 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 20 Jun 2024 12:16:16 +1000 Subject: [PATCH] Fix times, minTimes, maxTimes, no times logic and add test cases for corresponding --- .../jmockit/ExpectationsBlockRewriter.java | 26 ++-- .../JMockitExpectationsToMockitoTest.java | 143 ++++++++++++++++++ 2 files changed, 159 insertions(+), 10 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..f93af4258 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,16 +128,22 @@ private void rewriteMethodBody(List expectationStatements) { removeExpectationsStatement(); } - // TODO doesn't cover cases with multiple times eg minTimes and maxTimes - this is typically rare scenario if (mockInvocationResults.getTimes() != null) { writeMethodVerification(invocation, mockInvocationResults.getTimes(), "times"); - } else if (mockInvocationResults.getMinTimes() != null) { - writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); - } else if (mockInvocationResults.getMaxTimes() != null) { - writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); - } else if (!hasExpectationsResults) { - // no times, no results - writeMethodVerification(invocation, null, null); + } else { + boolean hasMinTimes = mockInvocationResults.getMinTimes() != null; + boolean hasMaxTimes = mockInvocationResults.getMaxTimes() != null; + if (hasMinTimes || hasMaxTimes) { + if (hasMinTimes) { + writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); + } + if (hasMaxTimes) { + writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); + } + } else if (!hasExpectationsResults) { + // no times, no results + writeMethodVerification(invocation, null, null); + } } } @@ -263,7 +269,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