From 533f37d91f27f395e44e1db7e1025cfe6033f9b5 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Sun, 4 Aug 2024 23:18:27 +1000 Subject: [PATCH] Add unit tests for JMockit Delegate to mockito migration. Also add test case for comments (#557) * Add unit tests for JMockit Delegate to mockito migration. Also add one test case for comments for JMockit Expectations proving that comments are not preserved. * Update JMockitDelegateToMockitoTest.java Modify test cases so that the tests fail with expected output of Delegate migration * Update JMockitExpectationsToMockitoTest.java Modify test to prove that comments migration is not supported within expectations, and this may be useful for someone who is adding this feature later. Disable test. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update JMockitDelegateToMockitoTest.java Modify tests to account for stubbing void methods using doAnswer instead of thenAnswer. Remove unnecessary imports * Tag tests with associated issue --------- Co-authored-by: Tim te Beek Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek --- .../jmockit/JMockitDelegateToMockitoTest.java | 366 ++++++++++++++++++ .../JMockitExpectationsToMockitoTest.java | 106 ++++- 2 files changed, 455 insertions(+), 17 deletions(-) create mode 100644 src/test/java/org/openrewrite/java/testing/jmockit/JMockitDelegateToMockitoTest.java diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitDelegateToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitDelegateToMockitoTest.java new file mode 100644 index 000000000..e4ad77bbd --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitDelegateToMockitoTest.java @@ -0,0 +1,366 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.jmockit; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.testing.jmockit.JMockitTestUtils.setDefaultParserSettings; + +/** + * At the moment, JMockit Delegates are not migrated to mockito. What I'm seeing is that they are being trashed + * with the template being printed out. These tests were written to try to replicate this issue, however I was unable to. + * They may help anyone who wants to add Delegate migration. + */ +@Disabled +@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/522") +class JMockitDelegateToMockitoTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + setDefaultParserSettings(spec); + } + + @DocumentExample + @Test + void whenNoArgsVoidMethod() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Delegate; + 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(); + result = new Delegate() { + public void wait() { + System.out.println("foo"); + } + }; + }}; + myObject.wait(); + } + } + """, + """ + import mockit.Delegate; + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.when; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + doAnswer(invocation -> { + System.out.println("foo"); + return null; + }).when(myObject).wait(); + myObject.wait(); + } + } + """ + ) + ); + } + + @Test + void whenHasArgsVoidMethod() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Delegate; + 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); + result = new Delegate() { + void wait(long timeoutMs) { + System.out.println("foo"); + System.out.println("bar"); + } + }; + }}; + myObject.wait(); + } + } + """, + """ + import mockit.Delegate; + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.anyLong; + import static org.mockito.Mockito.when; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + doAnswer(invocation -> { + System.out.println("foo"); + System.out.println("bar"); + return null; + }).when(myObject).wait(anyLong()); + myObject.wait(); + } + } + """ + ) + ); + } + + @Test + void whenNoArgsNonVoidMethod() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Delegate; + 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 + Object myObject; + + void test() { + new Expectations() {{ + myObject.toString(); + result = new Delegate() { + String toString() { + String a = "foo"; + return a + "bar"; + } + }; + }}; + assertEquals("foobar", myObject.toString()); + } + } + """, + """ + import mockit.Delegate; + 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 + Object myObject; + + void test() { + when(myObject.toString()).thenAnswer(invocation -> { + String a = "foo"; + return a + "bar"; + }); + assertEquals("foobar", myObject.toString()); + } + } + """ + ) + ); + } + + @Test + void whenMultipleStatementsWithAnnotation() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Delegate; + 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 + Object myObject; + + void test() { + new Expectations() {{ + myObject.hashCode(); + result = 100; + myObject.toString(); + result = new Delegate() { + @SuppressWarnings("unused") + String toString() { + String a = "foo"; + return a + "bar"; + } + }; + }}; + assertEquals(100, myObject.hashCode()); + assertEquals("foobar", myObject.toString()); + } + } + """, + """ + import mockit.Delegate; + 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 + Object myObject; + + void test() { + when(myObject.hashCode()).thenReturn(100); + when(myObject.toString()).thenAnswer(invocation -> { + String a = "foo"; + return a + "bar"; + }); + assertEquals(100, myObject.hashCode()); + assertEquals("foobar", myObject.toString()); + } + } + """ + ) + ); + } + + @Test + void whenClassArgumentMatcher() { + //language=java + rewriteRun( + java( + """ + import java.util.List; + + class MyObject { + public String getSomeField(List input) { + return "X"; + } + public String getSomeOtherField(Object input) { + return "Y"; + } + } + """ + ), + java( + """ + import java.util.ArrayList; + import java.util.List; + + import mockit.Delegate; + import mockit.Mocked; + import mockit.Expectations; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + new Expectations() {{ + myObject.getSomeField((List) any); + result = new Delegate() { + String getSomeOtherField(List input) { + input.add("foo"); + return input.toString(); + } + }; + }}; + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); + } + } + """, + """ + import java.util.ArrayList; + import java.util.List; + + import mockit.Delegate; + + import static org.mockito.Mockito.anyList; + import static org.mockito.Mockito.when; + + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + when(myObject.getSomeField(anyList())).thenAnswer(invocation -> { + List input = invocation.getArgument(0); + input.add("foo"); + return input.toString(); + }); + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); + } + } + """ + ) + ); + } + +} 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 fcc8bece2..c39233d20 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.testing.jmockit; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.test.RecipeSpec; @@ -41,7 +42,7 @@ void whenTimesAndResult() { 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) @@ -188,8 +189,8 @@ void whenHasResultNoTimes() { import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - - import static org.junit.jupiter.api.Assertions.assertEquals; + + import static org.junit.jupiter.api.Assertions.assertEquals; @ExtendWith(JMockitExtension.class) class MyTest { @@ -209,8 +210,8 @@ 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.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -1064,7 +1065,7 @@ void whenMinTimes() { //language=java rewriteRun( java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; @@ -1074,7 +1075,7 @@ void whenMinTimes() { class MyTest { @Mocked Object myObject; - + void test() { new Expectations() {{ myObject.wait(anyLong, anyInt); @@ -1084,7 +1085,7 @@ void test() { } } """, - """ + """ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -1095,7 +1096,7 @@ void test() { class MyTest { @Mock Object myObject; - + void test() { myObject.wait(10L, 10); verify(myObject, atLeast(2)).wait(anyLong(), anyInt()); @@ -1111,7 +1112,7 @@ void whenMaxTimes() { //language=java rewriteRun( java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; @@ -1121,7 +1122,7 @@ void whenMaxTimes() { class MyTest { @Mocked Object myObject; - + void test() { new Expectations() {{ myObject.wait(anyLong, anyInt); @@ -1131,7 +1132,7 @@ void test() { } } """, - """ + """ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -1142,7 +1143,7 @@ void test() { class MyTest { @Mock Object myObject; - + void test() { myObject.wait(10L, 10); verify(myObject, atMost(5)).wait(anyLong(), anyInt()); @@ -1158,7 +1159,7 @@ void whenMinTimesMaxTimes() { //language=java rewriteRun( java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; @@ -1168,7 +1169,7 @@ void whenMinTimesMaxTimes() { class MyTest { @Mocked Object myObject; - + void test() { new Expectations() {{ myObject.wait(anyLong, anyInt); @@ -1179,7 +1180,7 @@ void test() { } } """, - """ + """ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -1190,7 +1191,7 @@ void test() { class MyTest { @Mock Object myObject; - + void test() { myObject.wait(10L, 10); verify(myObject, atLeast(1)).wait(anyLong(), anyInt()); @@ -1474,4 +1475,75 @@ void test() { ) ); } + + @Disabled // comment migration not supported yet + @Test + void whenComments() { + //language=java + rewriteRun( + java( + """ + class MyObject { + public String getSomeStringField() { + return "X"; + } + } + """ + ), + 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; + import static org.junit.jupiter.api.Assertions.assertNull; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + new Expectations() {{ + // comments for this line below + myObject.getSomeStringField(); + result = "a"; + }}; + assertEquals("a", myObject.getSomeStringField()); + new Expectations() {{ + myObject.getSomeStringField(); + result = "b"; + }}; + assertEquals("b", myObject.getSomeStringField()); + } + } + """, + """ + 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.junit.jupiter.api.Assertions.assertNull; + import static org.mockito.Mockito.when; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + // comments for this line below + when(myObject.getSomeStringField()).thenReturn("a"); + assertEquals("a", myObject.getSomeStringField()); + when(myObject.getSomeStringField()).thenReturn("b"); + assertEquals("b", myObject.getSomeStringField()); + } + } + """ + ) + ); + } }