From 5042d5f43806708136247b96777cbaea1a3bad63 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Wed, 19 Jun 2024 13:33:47 +1000 Subject: [PATCH 1/3] Ensure Jmockit expectations with no times or result transform to a mockito verify --- .../jmockit/ExpectationsBlockRewriter.java | 46 ++++--- .../JMockitExpectationsToMockitoTest.java | 120 +++++++++++++++++- 2 files changed, 148 insertions(+), 18 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 c45b0b798..a1522518d 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java @@ -22,6 +22,7 @@ import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.tree.*; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; @@ -118,21 +119,25 @@ private void rewriteMethodBody(List expectationStatements) { return; } J.MethodInvocation invocation = (J.MethodInvocation) expectationStatements.get(0); - if (!mockInvocationResults.getResults().isEmpty()) { + boolean hasExpectationsResults = !mockInvocationResults.getResults().isEmpty(); + if (hasExpectationsResults) { // rewrite the statement to mockito if there are results rewriteExpectationResult(mockInvocationResults.getResults(), invocation); } else if (nextStatementCoordinates.isReplacement()) { // if there are no results and the Expectations block is not yet replaced, remove it 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"); - } - if (mockInvocationResults.getMinTimes() != null) { + } else if (mockInvocationResults.getMinTimes() != null) { writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); - } - if (mockInvocationResults.getMaxTimes() != null) { + } else if (mockInvocationResults.getMaxTimes() != null) { writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); + } else if (!hasExpectationsResults) { + // no times, no results + writeMethodVerification(invocation, null, null); } } @@ -182,18 +187,22 @@ private void removeExpectationsStatement() { methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded).getCoordinates().after(); } - private void writeMethodVerification(J.MethodInvocation invocation, Expression times, String verificationMode) { + private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Expression times, @Nullable String verificationMode) { String fqn = getInvocationSelectFullyQualifiedClassName(invocation); if (fqn == null) { // cannot write a verification statement for an invocation without a select field return; } visitor.maybeAddImport("org.mockito.Mockito", "verify"); - visitor.maybeAddImport("org.mockito.Mockito", verificationMode); + if (verificationMode != null) { + visitor.maybeAddImport("org.mockito.Mockito", verificationMode); + } List templateParams = new ArrayList<>(); templateParams.add(invocation.getSelect()); - templateParams.add(times); + if (times != null) { + templateParams.add(times); + } templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); @@ -251,17 +260,20 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil templateBuilder.append(templateField); } - private static String getVerifyTemplate(List arguments, String fqn, String verificationMode, List templateParams) { + 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(").#{}("); // verify(object, times(2)).method( + if (arguments.isEmpty()) { - return "verify(#{any(" + fqn + ")}, " - + verificationMode - + "(#{any(int)})).#{}();"; + templateBuilder.append(");"); // verify(object, times(2)).method(); + return templateBuilder.toString(); } - StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}, " - + verificationMode - + "(#{any(int)})).#{}("); + boolean hasArgument = false; - for (Expression argument : arguments) { + for (Expression argument : arguments) { // verify(object, times(2).method(anyLong(), any Int() if (argument instanceof J.Empty) { continue; } else if (argument instanceof J.Literal) { @@ -276,7 +288,7 @@ private static String getVerifyTemplate(List arguments, String fqn, if (hasArgument) { templateBuilder.delete(templateBuilder.length() - 2, templateBuilder.length()); } - templateBuilder.append(");"); + templateBuilder.append(");"); // verify(object, times(2).method(anyLong(), any Int()); return templateBuilder.toString(); } 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 72d8cfe7d..fc86ec59e 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -44,7 +44,7 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void voidResult() { + void whenNoResultNoTimes() { //language=java rewriteRun( java( @@ -71,6 +71,55 @@ void test() { 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).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @DocumentExample + @Test + void whenNoResultNoTimesNoArgs() { + //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(); + }}; + 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.verify; @ExtendWith(MockitoExtension.class) class MyTest { @@ -79,6 +128,7 @@ class MyTest { void test() { myObject.wait(10L, 10); + verify(myObject).wait(); } } """ @@ -585,6 +635,73 @@ void test() { ); } + @Test + void whenNoArguments() { + //language=java + rewriteRun( + java( + """ + import java.util.List; + + class MyObject { + public String getSomeField() { + return "X"; + } + } + """ + ), + java( + """ + import java.util.ArrayList; + import java.util.List; + + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + import static org.junit.jupiter.api.Assertions.assertNull; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + new Expectations() {{ + myObject.getSomeField(); + result = null; + }}; + assertNull(myObject.getSomeField()); + } + } + """, + """ + import java.util.ArrayList; + import java.util.List; + + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.junit.jupiter.api.Assertions.assertNull; + import static org.mockito.Mockito.when; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + when(myObject.getSomeField()).thenReturn(null); + assertNull(myObject.getSomeField()); + } + } + """ + ) + ); + } + @Test void whenMixedArgumentMatcher() { //language=java @@ -989,6 +1106,7 @@ void test() { assertNull(myOtherObject.getSomeObjectField()); myObject.wait(10L, 10); assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + verify(myObject).wait(anyLong(), anyInt()); } } """ From dd1b2c42bc72aa83422301529da5aa7d5c851747 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 19 Jun 2024 14:47:15 +0200 Subject: [PATCH 2/3] Minor polish to text blocks --- .../JMockitExpectationsToMockitoTest.java | 130 +++++++++--------- 1 file changed, 65 insertions(+), 65 deletions(-) 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 fc86ec59e..e8f8fa1eb 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -209,19 +209,19 @@ public int getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -240,7 +240,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @@ -275,19 +275,19 @@ public String getSomeField(String s) { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(anyString); @@ -301,7 +301,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -335,21 +335,21 @@ public String getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + String expected = "expected"; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -363,17 +363,17 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + String expected = "expected"; - + void test() { when(myObject.getSomeField()).thenReturn(expected); assertEquals(expected, myObject.getSomeField()); @@ -398,19 +398,19 @@ public Object getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertNotNull; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -424,7 +424,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.when; @@ -579,7 +579,7 @@ public String getSomeOtherField(Object input) { """ ), java( - """ + """ import java.util.ArrayList; import java.util.List; @@ -594,7 +594,7 @@ public String getSomeOtherField(Object input) { class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField((List) any); @@ -622,7 +622,7 @@ void test() { class MyTest { @Mock MyObject myObject; - + void test() { when(myObject.getSomeField(anyList())).thenReturn(null); when(myObject.getSomeOtherField(any(Object.class))).thenReturn(null); @@ -651,7 +651,7 @@ public String getSomeField() { """ ), java( - """ + """ import java.util.ArrayList; import java.util.List; @@ -666,7 +666,7 @@ public String getSomeField() { class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -691,7 +691,7 @@ void test() { class MyTest { @Mock MyObject myObject; - + void test() { when(myObject.getSomeField()).thenReturn(null); assertNull(myObject.getSomeField()); @@ -718,7 +718,7 @@ public String getSomeField(String s, String s2, String s3, long l1) { """ ), java( - """ + """ import java.util.ArrayList; import java.util.List; @@ -733,7 +733,7 @@ public String getSomeField(String s, String s2, String s3, long l1) { class MyTest { @Mocked MyObject myObject; - + void test() { String bazz = "bazz"; new Expectations() {{ @@ -759,7 +759,7 @@ void test() { class MyTest { @Mock MyObject myObject; - + void test() { String bazz = "bazz"; when(myObject.getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L))).thenReturn(null); @@ -789,31 +789,31 @@ public String getString() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { String a = "a"; String s = "s"; - + new Expectations() {{ myObject.getSomeField(anyString); result = s; - + myObject.getString(); result = a; }}; - + assertEquals("s", myObject.getSomeField("foo")); assertEquals("a", myObject.getString()); } @@ -823,7 +823,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -836,11 +836,11 @@ class MyTest { void test() { String a = "a"; String s = "s"; - + when(myObject.getSomeField(anyString())).thenReturn(s); - + when(myObject.getString()).thenReturn(a); - + assertEquals("s", myObject.getSomeField("foo")); assertEquals("a", myObject.getString()); } @@ -864,19 +864,19 @@ public String getSomeField(String s) { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { String a = "a"; new Expectations() {{ @@ -885,7 +885,7 @@ void test() { String b = "b"; result = s; }}; - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -894,7 +894,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -909,7 +909,7 @@ void test() { String s = "s"; String b = "b"; when(myObject.getSomeField(anyString())).thenReturn(s); - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -923,17 +923,17 @@ void whenTimes() { //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); @@ -944,7 +944,7 @@ void test() { } } """, - """ + """ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -955,7 +955,7 @@ void test() { class MyTest { @Mock Object myObject; - + void test() { myObject.wait(10L, 10); myObject.wait(10L, 10); @@ -986,14 +986,14 @@ public String getSomeField() { import mockit.Tested; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Tested MyObject myObject; - + void test() { new Expectations(myObject) {{ myObject.getSomeField(); @@ -1007,15 +1007,15 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; - + @ExtendWith(MockitoExtension.class) class MyTest { @InjectMocks MyObject myObject; - + void test() { when(myObject.getSomeField()).thenReturn("foo"); assertEquals("foo", myObject.getSomeField()); @@ -1060,10 +1060,10 @@ public void doSomething() {} class MyTest { @Mocked Object myObject; - + @Mocked MyObject myOtherObject; - + void test() { new Expectations() {{ myObject.hashCode(); @@ -1106,7 +1106,7 @@ void test() { assertNull(myOtherObject.getSomeObjectField()); myObject.wait(10L, 10); assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); - verify(myObject).wait(anyLong(), anyInt()); + verify(myObject).wait(anyLong(), anyInt()); } } """ From ebdbf2356d97ba6291eedae620efe4a750f99987 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Thu, 20 Jun 2024 12:30:32 +1000 Subject: [PATCH 3/3] 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