From 06592422338ec9141249ad79a32ced56756f5892 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Thu, 27 Jun 2024 17:30:22 +1000 Subject: [PATCH 01/13] First draft of adding feature to migration JMockit Verifications to Mockito verify statements. Tests are failing, need to fix and also add tests once they pass. --- ...ewriter.java => JMockitBlockRewriter.java} | 49 +- ...ockito.java => JMockitBlockToMockito.java} | 40 +- .../testing/jmockit/JMockitBlockType.java | 16 + .../java/testing/jmockit/JMockitUtils.java | 25 +- .../jmockit/SetupStatementsRewriter.java | 2 +- .../resources/META-INF/rewrite/jmockit.yml | 2 +- .../JMockitExpectationsToMockitoTest.java | 300 ++++-- .../JMockitVerificationsToMockitoTest.java | 949 ++++++++++++++++++ 8 files changed, 1246 insertions(+), 137 deletions(-) rename src/main/java/org/openrewrite/java/testing/jmockit/{ExpectationsBlockRewriter.java => JMockitBlockRewriter.java} (89%) rename src/main/java/org/openrewrite/java/testing/jmockit/{JMockitExpectationsToMockito.java => JMockitBlockToMockito.java} (62%) create mode 100644 src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java create mode 100644 src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java similarity index 89% rename from src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java rename to src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index ae2dce1ee..682c98526 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -26,7 +26,7 @@ import java.util.ArrayList; import java.util.List; -class ExpectationsBlockRewriter { +class JMockitBlockRewriter { private static final String WHEN_TEMPLATE_PREFIX = "when(#{any()})."; private static final String RETURN_TEMPLATE_PREFIX = "thenReturn("; @@ -41,6 +41,7 @@ private static String getObjectTemplateField(String fqn) { private final JavaVisitor visitor; private final ExecutionContext ctx; private final J.NewClass newExpectations; + private final JMockitBlockType blockType; // index of the Expectations block in the method body private final int bodyStatementIndex; private J.Block methodBody; @@ -56,18 +57,19 @@ boolean isExpectationsRewriteFailed() { // used with bodyStatementIndex to obtain the coordinates of the next statement to be written private int numStatementsAdded = 0; - ExpectationsBlockRewriter(JavaVisitor visitor, ExecutionContext ctx, J.Block methodBody, - J.NewClass newExpectations, int bodyStatementIndex) { + JMockitBlockRewriter(JavaVisitor visitor, ExecutionContext ctx, J.Block methodBody, + J.NewClass newExpectations, int bodyStatementIndex, JMockitBlockType blockType) { this.visitor = visitor; this.ctx = ctx; this.methodBody = methodBody; this.newExpectations = newExpectations; this.bodyStatementIndex = bodyStatementIndex; + this.blockType = blockType; nextStatementCoordinates = newExpectations.getCoordinates().replace(); } J.Block rewriteMethodBody() { - visitor.maybeRemoveImport("mockit.Expectations"); + visitor.maybeRemoveImport(blockType.getFqn()); // eg mockit.Expectations assert newExpectations.getBody() != null; J.Block expectationsBlock = (J.Block) newExpectations.getBody().getStatements().get(0); @@ -159,15 +161,7 @@ private void rewriteExpectationResult(List results, J.MethodInvocati templateParams.add(invocation); templateParams.addAll(results); - methodBody = JavaTemplate.builder(template) - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) - .staticImports("org.mockito.Mockito.*") - .build() - .apply( - new Cursor(visitor.getCursor(), methodBody), - nextStatementCoordinates, - templateParams.toArray() - ); + methodBody = getMockitoTemplate(template, templateParams, nextStatementCoordinates); if (!nextStatementCoordinates.isReplacement()) { numStatementsAdded += 1; } @@ -186,10 +180,12 @@ private void removeExpectationsStatement() { nextStatementCoordinates ); - // the next statement coordinates are directly after the most recently added statement, or the first statement - // of the test method body if the Expectations block was the first statement - nextStatementCoordinates = bodyStatementIndex == 0 ? methodBody.getCoordinates().firstStatement() : - methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded).getCoordinates().after(); + // TODO: Removing this below doesn't change anything but may affect in test case where we have no args in first + // expectation and then there are args in next expectation in same test method +// the next statement coordinates are directly after the most recently added statement, or the first statement +// // of the test method body if the Expectations block was the first statement +// nextStatementCoordinates = bodyStatementIndex == 0 ? methodBody.getCoordinates().firstStatement() : +// methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded).getCoordinates().after(); } private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Expression times, @Nullable String verificationMode) { @@ -211,14 +207,17 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); - methodBody = JavaTemplate.builder(verifyTemplate) + methodBody = getMockitoTemplate(verifyTemplate, templateParams, methodBody.getCoordinates().lastStatement()); + } + + private J.Block getMockitoTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { + return JavaTemplate.builder(verifyTemplate) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) .staticImports("org.mockito.Mockito.*") - .imports(fqn) .build() .apply( new Cursor(visitor.getCursor(), methodBody), - methodBody.getCoordinates().lastStatement(), + rewriteCoords, templateParams.toArray() ); } @@ -268,17 +267,17 @@ 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(", ").append(verificationMode).append("(#{any(int)})"); // verify(object, times(2) + templateBuilder.append(", ").append(verificationMode).append("(#{any(int)})"); // eg verify(object, times(2) } - templateBuilder.append(").#{}("); // verify(object, times(2)).method( + templateBuilder.append(").#{}("); // eg verify(object, times(2)).method( if (arguments.isEmpty()) { - templateBuilder.append(");"); // verify(object, times(2)).method(); + templateBuilder.append(");"); // eg verify(object, times(2)).method(); <- no args return templateBuilder.toString(); } boolean hasArgument = false; - for (Expression argument : arguments) { // verify(object, times(2).method(anyLong(), any Int() + for (Expression argument : arguments) { // eg verify(object, times(2).method(anyLong(), anyInt() if (argument instanceof J.Empty) { continue; } else if (argument instanceof J.Literal) { @@ -293,7 +292,7 @@ private static String getVerifyTemplate(List arguments, String fqn, if (hasArgument) { templateBuilder.delete(templateBuilder.length() - 2, templateBuilder.length()); } - templateBuilder.append(");"); // verify(object, times(2).method(anyLong(), any Int()); + templateBuilder.append(");"); // eg verify(object, times(2).method(anyLong(), anyInt()); return templateBuilder.toString(); } diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockito.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java similarity index 62% rename from src/main/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockito.java rename to src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java index e841aa033..4ea93dcfe 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockito.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java @@ -27,27 +27,31 @@ import org.openrewrite.java.tree.Statement; import java.util.List; +import java.util.Optional; @Value @EqualsAndHashCode(callSuper = false) -public class JMockitExpectationsToMockito extends Recipe { +public class JMockitBlockToMockito extends Recipe { + @Override public String getDisplayName() { - return "Rewrite JMockit Expectations"; + return "Rewrite JMockit Expectations and Verifications"; } @Override public String getDescription() { - return "Rewrites JMockit `Expectations` blocks to Mockito statements."; + return "Rewrites JMockit `Expectations and Verifications` blocks to Mockito statements."; } @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("mockit.Expectations", false), - new RewriteExpectationsVisitor()); + return Preconditions.check(Preconditions.or( + new UsesType<>(JMockitBlockType.Expectations.getFqn(), false), + new UsesType<>(JMockitBlockType.Verifications.getFqn(), false) + ), new RewriteJMockitBlockVisitor()); } - private static class RewriteExpectationsVisitor extends JavaIsoVisitor { + private static class RewriteJMockitBlockVisitor extends JavaIsoVisitor { @Override public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { @@ -63,17 +67,19 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl int bodyStatementIndex = 0; // iterate over each statement in the method body, find Expectations blocks and rewrite them while (bodyStatementIndex < statements.size()) { - if (!JMockitUtils.isValidExpectationsNewClassStatement(statements.get(bodyStatementIndex))) { - bodyStatementIndex += 1; - continue; - } - ExpectationsBlockRewriter ebr = new ExpectationsBlockRewriter(this, ctx, methodBody, - ((J.NewClass) statements.get(bodyStatementIndex)), bodyStatementIndex); - methodBody = ebr.rewriteMethodBody(); - statements = methodBody.getStatements(); - // if the expectations rewrite failed, skip the next statement - if (ebr.isExpectationsRewriteFailed()) { - bodyStatementIndex += 1; + Statement s = statements.get(bodyStatementIndex); + Optional blockType = JMockitUtils.getJMockitBlock(s); + if (blockType.isPresent()) { + JMockitBlockRewriter ebr = new JMockitBlockRewriter(this, ctx, methodBody, + ((J.NewClass) s), bodyStatementIndex, blockType.get()); + methodBody = ebr.rewriteMethodBody(); + statements = methodBody.getStatements(); + // if the expectations rewrite failed, skip the next statement + if (ebr.isExpectationsRewriteFailed()) { + bodyStatementIndex++; + } + } else { + bodyStatementIndex++; } } return md.withBody(methodBody); diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java new file mode 100644 index 000000000..c49836bf7 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java @@ -0,0 +1,16 @@ +package org.openrewrite.java.testing.jmockit; + +enum JMockitBlockType { + + Expectations("mockit.Expectations"), Verifications("mockit.Verifications"); // Add NonStrictExpectations later + + private final String fqn; + + JMockitBlockType(String fqn) { + this.fqn = fqn; + } + + String getFqn() { + return fqn; + } +} diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java index 79d21e91c..ff4c84b16 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java @@ -19,20 +19,31 @@ import org.openrewrite.java.tree.Statement; import org.openrewrite.java.tree.TypeUtils; +import java.util.Optional; + +import static java.util.Optional.empty; + class JMockitUtils { - static boolean isValidExpectationsNewClassStatement(Statement s) { + + static Optional getJMockitBlock(Statement s) { if (!(s instanceof J.NewClass)) { - return false; + return empty(); } J.NewClass nc = (J.NewClass) s; if (!(nc.getClazz() instanceof J.Identifier)) { - return false; + return empty(); } J.Identifier clazz = (J.Identifier) nc.getClazz(); - if (!TypeUtils.isAssignableTo("mockit.Expectations", clazz.getType())) { - return false; + + for (JMockitBlockType blockType : JMockitBlockType.values()) { + if (TypeUtils.isAssignableTo(blockType.getFqn(), clazz.getType())) { + // JMockit block should be composed of a block within another block + if (nc.getBody() != null && nc.getBody().getStatements().size() == 1) { + return Optional.of(blockType); + } + } } - // Expectations block should be composed of a block within another block - return nc.getBody() != null && nc.getBody().getStatements().size() == 1; + return empty(); } + } diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java index 4fd9a54f2..ed348c1f9 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java @@ -38,7 +38,7 @@ J.Block rewriteMethodBody() { List statements = methodBody.getStatements(); // iterate over each statement in the method body, find Expectations blocks and rewrite them for (Statement s : statements) { - if (!JMockitUtils.isValidExpectationsNewClassStatement(s)) { + if (!JMockitUtils.getJMockitBlock(s).isPresent()) { continue; } J.NewClass nc = (J.NewClass) s; diff --git a/src/main/resources/META-INF/rewrite/jmockit.yml b/src/main/resources/META-INF/rewrite/jmockit.yml index 1b4651ae2..4178ebcd4 100644 --- a/src/main/resources/META-INF/rewrite/jmockit.yml +++ b/src/main/resources/META-INF/rewrite/jmockit.yml @@ -22,7 +22,7 @@ tags: - testing - jmockit recipeList: - - org.openrewrite.java.testing.jmockit.JMockitExpectationsToMockito + - org.openrewrite.java.testing.jmockit.JMockitBlockToMockito - org.openrewrite.java.testing.jmockit.JMockitAnnotatedArgumentToMockito - org.openrewrite.java.ChangeType: oldFullyQualifiedTypeName: mockit.Mocked 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 69f4627fe..9d5977deb 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -71,7 +71,7 @@ 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) @@ -118,7 +118,7 @@ void test() { 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) @@ -155,7 +155,7 @@ public String getSomeField() { 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) @@ -176,7 +176,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.assertNull; import static org.mockito.Mockito.when; @@ -214,14 +214,14 @@ public int getSomeField() { 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; @@ -280,14 +280,14 @@ public String getSomeField(String s) { 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; @@ -340,16 +340,16 @@ public String getSomeField() { 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()); @@ -403,14 +403,14 @@ public Object getSomeField() { 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; @@ -518,7 +518,7 @@ public String getSomeField() { 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) @@ -567,7 +567,7 @@ void whenClassArgumentMatcher() { java( """ import java.util.List; - + class MyObject { public String getSomeField(List input) { return "X"; @@ -582,19 +582,19 @@ public String getSomeOtherField(Object input) { """ 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((List) any); @@ -610,19 +610,19 @@ void test() { """ 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.*; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + void test() { when(myObject.getSomeField(anyList())).thenReturn(null); when(myObject.getSomeOtherField(any(Object.class))).thenReturn(null); @@ -642,7 +642,7 @@ void whenNoArguments() { java( """ import java.util.List; - + class MyObject { public String getSomeField() { return "X"; @@ -654,19 +654,19 @@ public String getSomeField() { """ 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(); @@ -679,19 +679,19 @@ void test() { """ 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()); @@ -709,7 +709,7 @@ void whenMixedArgumentMatcher() { java( """ import java.util.List; - + class MyObject { public String getSomeField(String s, String s2, String s3, long l1) { return "X"; @@ -721,19 +721,19 @@ public String getSomeField(String s, String s2, String s3, long l1) { """ 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() { String bazz = "bazz"; new Expectations() {{ @@ -747,19 +747,19 @@ void test() { """ 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.*; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + void test() { String bazz = "bazz"; when(myObject.getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L))).thenReturn(null); @@ -778,7 +778,7 @@ void whenSetupStatements() { java( """ class MyObject { - + public String getSomeField(String s) { return "X"; } @@ -794,26 +794,26 @@ public String getString() { 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()); } @@ -869,14 +869,14 @@ public String getSomeField(String s) { 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")); } } @@ -928,19 +928,20 @@ void whenTimes() { 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); - times = 2; + times = 3; }}; myObject.wait(10L, 10); myObject.wait(10L, 10); + myObject.wait(10L, 10); } } """, @@ -948,18 +949,19 @@ 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); myObject.wait(10L, 10); - verify(myObject, times(2)).wait(anyLong(), anyInt()); + myObject.wait(10L, 10); + verify(myObject, times(3)).wait(anyLong(), anyInt()); } } """ @@ -1129,14 +1131,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(); @@ -1150,15 +1152,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()); @@ -1195,18 +1197,18 @@ public void doSomething() {} 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 Object myObject; - + @Mocked MyObject myOtherObject; - + void test() { new Expectations() {{ myObject.hashCode(); @@ -1256,4 +1258,130 @@ void test() { ) ); } + + @Test + void whenMultipleExpectations() { + //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() {{ + 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() { + when(myObject.getSomeStringField()).thenReturn("a"); + assertEquals("a", myObject.getSomeStringField()); + when(myObject.getSomeStringField()).thenReturn("b"); + assertEquals("b", myObject.getSomeStringField()); + } + } + """ + ) + ); + } + + @Test + void whenMultipleExpectationsNoResults() { + //language=java + rewriteRun( + 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 + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong); + }}; + myObject.wait(1L); + new Expectations() {{ + myObject.wait(); + }}; + myObject.wait(); + } + } + """, + """ + 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.anyLong; + import static org.mockito.Mockito.verify; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(1L); + myObject.wait(); + verify(myObject).wait(anyLong()); + verify(myObject).wait(); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java new file mode 100644 index 000000000..546d90045 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -0,0 +1,949 @@ +/* + * 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.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class JMockitVerificationsToMockitoTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion() + .logCompilationWarningsAndErrors(true) + .classpathFromResources(new InMemoryExecutionContext(), + "junit-jupiter-api-5.9", + "jmockit-1.49", + "mockito-core-3.12", + "mockito-junit-jupiter-3.12" + )) + .recipeFromResource( + "/META-INF/rewrite/jmockit.yml", + "org.openrewrite.java.testing.jmockit.JMockitToMockito" + ); + } + + @DocumentExample + @Test + void whenNoTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Verifications; + 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() { + myObject.wait(10L, 10); + new Verifications() {{ + myObject.wait(anyLong, anyInt); + }}; + } + } + """, + """ + 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 whenNoTimesNoArgs() { + //language=java + rewriteRun( + java( + """ + import mockit.Verifications; + 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() { + myObject.wait(10L, 10); + new Verifications() {{ + myObject.wait(); + }}; + } + } + """, + """ + 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 { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject).wait(); + } + } + """ + ) + ); + } + + + @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.Verifications; + 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() { + assertNull(myObject.getSomeField(new ArrayList<>())); + assertNull(myObject.getSomeOtherField(new Object())); + new Verifications() {{ + myObject.getSomeField((List) any); + myObject.getSomeOtherField((Object) any); + }}; + } + } + """, + """ + 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.any; + import static org.mockito.Mockito.anyList; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + assertNull(myObject.getSomeField(new ArrayList<>())); + assertNull(myObject.getSomeOtherField(new Object())); + verify(myObject).getSomeField(anyList()); + verify(myObject).getSomeOtherField(any(Object.class)); + } + } + """ + ) + ); + } + + @Test + void whenTimesNoArgs() { + //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.Verifications; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + myObject.getSomeField(); + new Verifications() {{ + 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.mockito.Mockito.verify; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + myObject.getSomeField(); + verify(myObject).getSomeField(); + } + } + """ + ) + ); + } + + @Test + void whenMixedArgumentMatcher() { + //language=java + rewriteRun( + java( + """ + import java.util.List; + + class MyObject { + public String getSomeField(String s, String s2, String s3, long l1) { + return "X"; + } + } + """ + ), + java( + """ + import java.util.ArrayList; + import java.util.List; + + import mockit.Verifications; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + String bazz = "bazz"; + myObject.getSomeField("foo", "bar", bazz, 10L); + new Verifications() {{ + myObject.getSomeField("foo", anyString, bazz, 10L); + }}; + } + } + """, + """ + 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.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + String bazz = "bazz"; + myObject.getSomeField("foo", "bar", bazz, 10L); + verify(myObject).getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L)); + } + } + """ + ) + ); + } + + @Test + void whenSetupStatements() { + //language=java + rewriteRun( + java( + """ + class MyObject { + + public String getSomeField(String s) { + return "X"; + } + public String getString() { + return "Y"; + } + } + """ + ), + java( + """ + import mockit.Verifications; + 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"; + assertEquals("s", myObject.getSomeField("foo")); + assertEquals("a", myObject.getString()); + new Verifications() {{ + myObject.getSomeField(anyString); + result = s; + + myObject.getString(); + result = a; + }}; + } + } + """, + """ + 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.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + String a = "a"; + String s = "s"; + assertEquals("s", myObject.getSomeField("foo")); + assertEquals("a", myObject.getString()); + verify(myObject).getSomeField(anyString()); + verify(myObject).getString(); + } + } + """ + ) + ); + } + + @Test + void whenSetupStatements2() { + //language=java + rewriteRun( + java( + """ + class MyObject { + public String getSomeField(String s) { + 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; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + String a = "a"; + new Expectations() {{ + myObject.getSomeField(anyString); + String s = "s"; + String b = "b"; + result = s; + }}; + + assertEquals("s", myObject.getSomeField("foo")); + } + } + """, + """ + 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; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + String a = "a"; + String s = "s"; + String b = "b"; + when(myObject.getSomeField(anyString())).thenReturn(s); + + assertEquals("s", myObject.getSomeField("foo")); + } + } + """ + ) + ); + } + + @Test + 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); + times = 3; + }}; + myObject.wait(10L, 10); + myObject.wait(10L, 10); + 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); + myObject.wait(10L, 10); + myObject.wait(10L, 10); + verify(myObject, times(3)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @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 + rewriteRun( + java( + """ + class MyObject { + public String getSomeField() { + return "X"; + } + } + """ + ), + java( + """ + import mockit.Expectations; + 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(); + result = "foo"; + }}; + assertEquals("foo", myObject.getSomeField()); + } + } + """, + """ + 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()); + } + } + """ + ) + ); + } + + @Test + void whenMultipleStatements() { + //language=java + rewriteRun( + java( + """ + class MyObject { + public String getSomeStringField(String input, long otherInput) { + return "X"; + } + public int getSomeIntField() { + return 0; + } + public Object getSomeObjectField() { + return new Object(); + } + public void doSomething() {} + } + """ + ), + 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 + Object myObject; + + @Mocked + MyObject myOtherObject; + + void test() { + new Expectations() {{ + myObject.hashCode(); + result = 10; + myOtherObject.getSomeObjectField(); + result = null; + myObject.wait(anyLong, anyInt); + myOtherObject.getSomeStringField(anyString, anyLong); + result = "foo"; + }}; + assertEquals(10, myObject.hashCode()); + assertNull(myOtherObject.getSomeObjectField()); + myObject.wait(10L, 10); + assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + } + } + """, + """ + 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.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + @Mock + MyObject myOtherObject; + + void test() { + when(myObject.hashCode()).thenReturn(10); + when(myOtherObject.getSomeObjectField()).thenReturn(null); + when(myOtherObject.getSomeStringField(anyString(), anyLong())).thenReturn("foo"); + assertEquals(10, myObject.hashCode()); + assertNull(myOtherObject.getSomeObjectField()); + myObject.wait(10L, 10); + assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + verify(myObject).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMultipleExpectations() { + //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() {{ + 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() { + when(myObject.getSomeStringField()).thenReturn("a"); + assertEquals("a", myObject.getSomeStringField()); + when(myObject.getSomeStringField()).thenReturn("b"); + assertEquals("b", myObject.getSomeStringField()); + } + } + """ + ) + ); + } + + @Test + void whenMultipleExpectationsNoResults() { + //language=java + rewriteRun( + 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 + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong); + }}; + myObject.wait(1L); + new Expectations() {{ + myObject.wait(); + }}; + myObject.wait(); + } + } + """, + """ + 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.anyLong; + import static org.mockito.Mockito.verify; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(1L); + myObject.wait(); + verify(myObject).wait(anyLong()); + verify(myObject).wait(); + } + } + """ + ) + ); + } +} From dd76d3d1b3c84052d641d28883d26682941eb0e0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 27 Jun 2024 10:53:30 +0200 Subject: [PATCH 02/13] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/testing/jmockit/JMockitBlockType.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java index c49836bf7..12cd0b634 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 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; enum JMockitBlockType { From d5ac4464f1eed316704d0284952d902f5046b7a5 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 00:28:10 +1000 Subject: [PATCH 03/13] Add more test cases, fix existing test cases, refactor method name. Some test still failing. --- .../testing/jmockit/JMockitBlockRewriter.java | 6 +- .../JMockitVerificationsToMockitoTest.java | 318 +++++------------- 2 files changed, 79 insertions(+), 245 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 682c98526..80314cd50 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -161,7 +161,7 @@ private void rewriteExpectationResult(List results, J.MethodInvocati templateParams.add(invocation); templateParams.addAll(results); - methodBody = getMockitoTemplate(template, templateParams, nextStatementCoordinates); + methodBody = rewriteTemplate(template, templateParams, nextStatementCoordinates); if (!nextStatementCoordinates.isReplacement()) { numStatementsAdded += 1; } @@ -207,10 +207,10 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); - methodBody = getMockitoTemplate(verifyTemplate, templateParams, methodBody.getCoordinates().lastStatement()); + methodBody = rewriteTemplate(verifyTemplate, templateParams, methodBody.getCoordinates().lastStatement()); } - private J.Block getMockitoTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { + private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { return JavaTemplate.builder(verifyTemplate) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) .staticImports("org.mockito.Mockito.*") diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index 546d90045..95f82de2b 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -160,21 +160,19 @@ public String getSomeOtherField(Object input) { import java.util.ArrayList; import java.util.List; - import mockit.Verifications; import mockit.Mocked; + import mockit.Verifications; 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() { - assertNull(myObject.getSomeField(new ArrayList<>())); - assertNull(myObject.getSomeOtherField(new Object())); + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); new Verifications() {{ myObject.getSomeField((List) any); myObject.getSomeOtherField((Object) any); @@ -189,19 +187,15 @@ 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.assertNull; - import static org.mockito.Mockito.any; - import static org.mockito.Mockito.anyList; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; void test() { - assertNull(myObject.getSomeField(new ArrayList<>())); - assertNull(myObject.getSomeOtherField(new Object())); + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); verify(myObject).getSomeField(anyList()); verify(myObject).getSomeOtherField(any(Object.class)); } @@ -215,17 +209,6 @@ void test() { void whenTimesNoArgs() { //language=java rewriteRun( - java( - """ - import java.util.List; - - class MyObject { - public String getSomeField() { - return "X"; - } - } - """ - ), java( """ import java.util.ArrayList; @@ -235,16 +218,18 @@ public String getSomeField() { import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked - MyObject myObject; + Object myObject; void test() { - myObject.getSomeField(); + myObject.wait(); + myObject.wait(); new Verifications() {{ - myObject.getSomeField(); + myObject.wait(); + times = 2; }}; } } @@ -253,20 +238,21 @@ void test() { import java.util.ArrayList; import java.util.List; + import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; 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 { @Mock - MyObject myObject; + Object myObject; void test() { - myObject.getSomeField(); - verify(myObject).getSomeField(); + myObject.wait(); + myObject.wait(); + verify(myObject, times(2)).wait(); } } """ @@ -363,8 +349,6 @@ public String getString() { 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 @@ -373,14 +357,11 @@ class MyTest { void test() { String a = "a"; String s = "s"; - assertEquals("s", myObject.getSomeField("foo")); - assertEquals("a", myObject.getString()); + myObject.getSomeField("foo"); + myObject.getString(); new Verifications() {{ - myObject.getSomeField(anyString); - result = s; - + myObject.getSomeField(anyString); myObject.getString(); - result = a; }}; } } @@ -390,7 +371,6 @@ void test() { import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -401,8 +381,8 @@ class MyTest { void test() { String a = "a"; String s = "s"; - assertEquals("s", myObject.getSomeField("foo")); - assertEquals("a", myObject.getString()); + myObject.getSomeField("foo"); + myObject.getString(); verify(myObject).getSomeField(anyString()); verify(myObject).getString(); } @@ -427,28 +407,22 @@ public String getSomeField(String s) { ), java( """ - import mockit.Expectations; + import mockit.Verifications; 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() {{ + String a = "a"; + myObject.getSomeField("foo"); + new Verifications() {{ myObject.getSomeField(anyString); - String s = "s"; - String b = "b"; - result = s; - }}; - - assertEquals("s", myObject.getSomeField("foo")); + }}; } } """, @@ -459,7 +433,7 @@ void test() { import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; - import static org.mockito.Mockito.when; + import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) class MyTest { @@ -468,11 +442,8 @@ class MyTest { void test() { String a = "a"; - String s = "s"; - String b = "b"; - when(myObject.getSomeField(anyString())).thenReturn(s); - - assertEquals("s", myObject.getSomeField("foo")); + myObject.getSomeField("foo"); + verify(myObject).getSomeField(anyString()); } } """ @@ -486,7 +457,7 @@ void whenTimes() { rewriteRun( java( """ - import mockit.Expectations; + import mockit.Verifications; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; @@ -497,13 +468,13 @@ class MyTest { Object myObject; void test() { - new Expectations() {{ - myObject.wait(anyLong, anyInt); - times = 3; - }}; myObject.wait(10L, 10); myObject.wait(10L, 10); myObject.wait(10L, 10); + new Verifications() {{ + myObject.wait(anyLong, anyInt); + times = 3; + }}; } } """, @@ -537,7 +508,7 @@ void whenMinTimes() { rewriteRun( java( """ - import mockit.Expectations; + import mockit.Verifications; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; @@ -548,11 +519,11 @@ class MyTest { Object myObject; void test() { - new Expectations() {{ + myObject.wait(10L, 10); + new Verifications() {{ myObject.wait(anyLong, anyInt); minTimes = 2; }}; - myObject.wait(10L, 10); } } """, @@ -584,7 +555,7 @@ void whenMaxTimes() { rewriteRun( java( """ - import mockit.Expectations; + import mockit.Verifications; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; @@ -595,11 +566,11 @@ class MyTest { Object myObject; void test() { - new Expectations() {{ + myObject.wait(10L, 10); + new Verifications() {{ myObject.wait(anyLong, anyInt); maxTimes = 5; }}; - myObject.wait(10L, 10); } } """, @@ -631,7 +602,7 @@ void whenMinTimesMaxTimes() { rewriteRun( java( """ - import mockit.Expectations; + import mockit.Verifications; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; @@ -642,12 +613,12 @@ class MyTest { Object myObject; void test() { - new Expectations() {{ + myObject.wait(10L, 10); + new Verifications() {{ myObject.wait(anyLong, anyInt); minTimes = 1; maxTimes = 3; }}; - myObject.wait(10L, 10); } } """, @@ -674,65 +645,6 @@ void test() { ); } - @Test - void whenSpy() { - //language=java - rewriteRun( - java( - """ - class MyObject { - public String getSomeField() { - return "X"; - } - } - """ - ), - java( - """ - import mockit.Expectations; - 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(); - result = "foo"; - }}; - assertEquals("foo", myObject.getSomeField()); - } - } - """, - """ - 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()); - } - } - """ - ) - ); - } - @Test void whenMultipleStatements() { //language=java @@ -755,14 +667,11 @@ public void doSomething() {} ), java( """ - import mockit.Expectations; + import mockit.Verifications; 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 @@ -772,19 +681,16 @@ class MyTest { MyObject myOtherObject; void test() { - new Expectations() {{ + myObject.hashCode(); + myOtherObject.getSomeObjectField(); + myObject.wait(10L, 10); + myOtherObject.getSomeStringField("bar", 10L); + new Verifications() {{ myObject.hashCode(); - result = 10; myOtherObject.getSomeObjectField(); - result = null; myObject.wait(anyLong, anyInt); myOtherObject.getSomeStringField(anyString, anyLong); - result = "foo"; - }}; - assertEquals(10, myObject.hashCode()); - assertNull(myOtherObject.getSomeObjectField()); - myObject.wait(10L, 10); - assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + }}; } } """, @@ -793,8 +699,6 @@ void test() { 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.*; @ExtendWith(MockitoExtension.class) @@ -806,14 +710,14 @@ class MyTest { MyObject myOtherObject; void test() { - when(myObject.hashCode()).thenReturn(10); - when(myOtherObject.getSomeObjectField()).thenReturn(null); - when(myOtherObject.getSomeStringField(anyString(), anyLong())).thenReturn("foo"); - assertEquals(10, myObject.hashCode()); - assertNull(myOtherObject.getSomeObjectField()); + myObject.hashCode(); + myOtherObject.getSomeObjectField(); myObject.wait(10L, 10); - assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + myOtherObject.getSomeStringField("bar", 10L); + verify(myObject).hashCode(); + verify(myOtherObject).getSomeObjectField(); verify(myObject).wait(anyLong(), anyInt()); + verify(myOtherObject).getSomeStringField(anyString(), anyLong()); } } """ @@ -822,101 +726,33 @@ void test() { } @Test - void whenMultipleExpectations() { - //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() {{ - 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() { - when(myObject.getSomeStringField()).thenReturn("a"); - assertEquals("a", myObject.getSomeStringField()); - when(myObject.getSomeStringField()).thenReturn("b"); - assertEquals("b", myObject.getSomeStringField()); - } - } - """ - ) - ); - } - - @Test - void whenMultipleExpectationsNoResults() { + //TODO Preferably Verifications should be replaced inline and not appended to end of the method + void whenMultipleVerifications() { //language=java rewriteRun( java( """ - import mockit.Expectations; + import mockit.Verifications; 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 Object myObject; void test() { - new Expectations() {{ - myObject.wait(anyLong); + myObject.wait(); + new Verifications() {{ + myObject.wait(); }}; myObject.wait(1L); - new Expectations() {{ - myObject.wait(); + myObject.wait(2L); + new Verifications() {{ + myObject.wait(anyLong); + times = 2; }}; - myObject.wait(); } } """, @@ -925,10 +761,7 @@ void test() { 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.anyLong; - import static org.mockito.Mockito.verify; + import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class MyTest { @@ -936,10 +769,11 @@ class MyTest { Object myObject; void test() { - myObject.wait(1L); myObject.wait(); - verify(myObject).wait(anyLong()); - verify(myObject).wait(); + myObject.wait(1L); + myObject.wait(2L); + verify(myObject).wait(); + verify(myObject, times(2)).wait(anyLong()); } } """ From 19bc094c62311e22cf88a97170a7c163f06edd14 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 02:42:51 +1000 Subject: [PATCH 04/13] Refactor everything to be simpler to understand, also remove empty new line between migrated mockit when statements as mockito statements are more compact than Expectations --- .../jmockit/ArgumentMatchersRewriter.java | 3 +- .../testing/jmockit/JMockitBlockRewriter.java | 108 +++++++++--------- .../java/testing/jmockit/JMockitUtils.java | 11 +- .../JMockitExpectationsToMockitoTest.java | 4 +- .../JMockitVerificationsToMockitoTest.java | 3 +- 5 files changed, 64 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java index 7d5efe007..8b24ee034 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java @@ -60,6 +60,7 @@ class ArgumentMatchersRewriter { } private static final Map PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER = new HashMap<>(); + static { PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER.put(JavaType.Primitive.Int, "anyInt"); PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER.put(JavaType.Primitive.Long, "anyLong"); @@ -81,7 +82,7 @@ class ArgumentMatchersRewriter { this.expectationsBlock = expectationsBlock; } - J.Block rewriteExpectationsBlock() { + J.Block rewriteJMockitBlock() { List newStatements = new ArrayList<>(expectationsBlock.getStatements().size()); for (Statement expectationStatement : expectationsBlock.getStatements()) { // for each statement, check if it's a method invocation and replace any argument matchers diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 80314cd50..47b1bc3c5 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -72,62 +72,57 @@ J.Block rewriteMethodBody() { visitor.maybeRemoveImport(blockType.getFqn()); // eg mockit.Expectations assert newExpectations.getBody() != null; - J.Block expectationsBlock = (J.Block) newExpectations.getBody().getStatements().get(0); - if (expectationsBlock.getStatements().isEmpty()) { + J.Block jmockitBlock = (J.Block) newExpectations.getBody().getStatements().get(0); + if (jmockitBlock.getStatements().isEmpty()) { // empty Expectations block, remove it - removeExpectationsStatement(); + removeBlock(); return methodBody; } // rewrite the argument matchers in the expectations block - ArgumentMatchersRewriter amr = new ArgumentMatchersRewriter(visitor, ctx, expectationsBlock); - expectationsBlock = amr.rewriteExpectationsBlock(); - - // iterate over the expectations statements and rebuild the method body - List expectationStatements = new ArrayList<>(); - for (Statement expectationStatement : expectationsBlock.getStatements()) { - if (expectationStatement instanceof J.MethodInvocation) { - // handle returns statements - J.MethodInvocation invocation = (J.MethodInvocation) expectationStatement; - if (invocation.getSelect() == null && invocation.getName().getSimpleName().equals("returns")) { - expectationStatements.add(expectationStatement); - continue; + ArgumentMatchersRewriter amr = new ArgumentMatchersRewriter(visitor, ctx, jmockitBlock); + jmockitBlock = amr.rewriteJMockitBlock(); + + // iterate over the statements and build a list of grouped method invocations and related statements eg times + List> methodInvocationsToRewrite = new ArrayList<>(); + int methodInvocationIdx = -1; + for (Statement jmockitBlockStatement : jmockitBlock.getStatements()) { + if (jmockitBlockStatement instanceof J.MethodInvocation) { + // ensure it's not a returns statement, we add that later to related statements + J.MethodInvocation invocation = (J.MethodInvocation) jmockitBlockStatement; + if (invocation.getSelect() != null && !invocation.getName().getSimpleName().equals("returns")) { + methodInvocationIdx++; + methodInvocationsToRewrite.add(new ArrayList<>()); } - // if a new method invocation is found, apply the template to the previous statements - if (!expectationStatements.isEmpty()) { - // apply template to build new method body - rewriteMethodBody(expectationStatements); + } - // reset statements for next expectation - expectationStatements = new ArrayList<>(); - } + if (methodInvocationIdx != -1) { + methodInvocationsToRewrite.get(methodInvocationIdx).add(jmockitBlockStatement); } - expectationStatements.add(expectationStatement); } - // handle the last statement - if (!expectationStatements.isEmpty()) { - rewriteMethodBody(expectationStatements); + // remove the jmockit block + if (nextStatementCoordinates.isReplacement()) { + removeBlock(); } + // now rewrite + methodInvocationsToRewrite.forEach(this::rewriteMethodInvocation); return methodBody; } - private void rewriteMethodBody(List expectationStatements) { - final MockInvocationResults mockInvocationResults = buildMockInvocationResults(expectationStatements); - if (mockInvocationResults == null || !(expectationStatements.get(0) instanceof J.MethodInvocation)) { - // invalid Expectations block, cannot rewrite + private void rewriteMethodInvocation(List statementsToRewrite) { + final MockInvocationResults mockInvocationResults = buildMockInvocationResults(statementsToRewrite); + if (mockInvocationResults == null) { + // invalid block, cannot rewrite expectationsRewriteFailed = true; return; } - J.MethodInvocation invocation = (J.MethodInvocation) expectationStatements.get(0); - 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(); + + J.MethodInvocation invocation = (J.MethodInvocation) statementsToRewrite.get(0); + boolean hasResults = !mockInvocationResults.getResults().isEmpty(); + if (hasResults) { + rewriteJMockitResult(mockInvocationResults.getResults(), invocation); } boolean hasTimes = false; @@ -143,12 +138,12 @@ private void rewriteMethodBody(List expectationStatements) { hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); } - if (!hasExpectationsResults && !hasTimes) { + if (!hasResults && !hasTimes) { writeMethodVerification(invocation, null, null); } } - private void rewriteExpectationResult(List results, J.MethodInvocation invocation) { + private void rewriteJMockitResult(List results, J.MethodInvocation invocation) { String template = getMockitoStatementTemplate(results); if (template == null) { // invalid template, cannot rewrite @@ -162,16 +157,15 @@ private void rewriteExpectationResult(List results, J.MethodInvocati templateParams.addAll(results); methodBody = rewriteTemplate(template, templateParams, nextStatementCoordinates); - if (!nextStatementCoordinates.isReplacement()) { - numStatementsAdded += 1; - } + numStatementsAdded++; - // the next statement coordinates are directly after the most recently written statement - nextStatementCoordinates = methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded) + // the next statement coordinates are directly after the most recently written statement, and subtracting the + // removed jmockit block + nextStatementCoordinates = methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1) .getCoordinates().after(); } - private void removeExpectationsStatement() { + private void removeBlock() { methodBody = JavaTemplate.builder("") .javaParser(JavaParser.fromJavaVersion()) .build() @@ -179,13 +173,11 @@ private void removeExpectationsStatement() { new Cursor(visitor.getCursor(), methodBody), nextStatementCoordinates ); - - // TODO: Removing this below doesn't change anything but may affect in test case where we have no args in first - // expectation and then there are args in next expectation in same test method -// the next statement coordinates are directly after the most recently added statement, or the first statement -// // of the test method body if the Expectations block was the first statement -// nextStatementCoordinates = bodyStatementIndex == 0 ? methodBody.getCoordinates().firstStatement() : -// methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded).getCoordinates().after(); + if (bodyStatementIndex == 0) { + nextStatementCoordinates = methodBody.getCoordinates().firstStatement(); + } else { + nextStatementCoordinates = methodBody.getStatements().get(bodyStatementIndex - 1).getCoordinates().after(); + } } private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Expression times, @Nullable String verificationMode) { @@ -207,7 +199,15 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); - methodBody = rewriteTemplate(verifyTemplate, templateParams, methodBody.getCoordinates().lastStatement()); + JavaCoordinates verifyCoordinates; + if (this.blockType == JMockitBlockType.Verifications) { + // for Verifications, replace the Verifications block + verifyCoordinates = nextStatementCoordinates; + } else { + // for Expectations put the verify at the end of the method + verifyCoordinates = methodBody.getCoordinates().lastStatement(); + } + methodBody = rewriteTemplate(verifyTemplate, templateParams, verifyCoordinates); } private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java index ff4c84b16..a11b5246b 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitUtils.java @@ -35,15 +35,16 @@ static Optional getJMockitBlock(Statement s) { } J.Identifier clazz = (J.Identifier) nc.getClazz(); + // JMockit block should be composed of a block within another block + if (nc.getBody() == null || nc.getBody().getStatements().size() != 1) { + return empty(); + } + for (JMockitBlockType blockType : JMockitBlockType.values()) { if (TypeUtils.isAssignableTo(blockType.getFqn(), clazz.getType())) { - // JMockit block should be composed of a block within another block - if (nc.getBody() != null && nc.getBody().getStatements().size() == 1) { - return Optional.of(blockType); - } + return Optional.of(blockType); } } return empty(); } - } 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 9d5977deb..9712a1e19 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -836,9 +836,7 @@ 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")); @@ -1377,7 +1375,7 @@ void test() { myObject.wait(1L); myObject.wait(); verify(myObject).wait(anyLong()); - verify(myObject).wait(); + verify(myObject).wait(); } } """ diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index 95f82de2b..0819adde5 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -726,7 +726,6 @@ void test() { } @Test - //TODO Preferably Verifications should be replaced inline and not appended to end of the method void whenMultipleVerifications() { //language=java rewriteRun( @@ -770,9 +769,9 @@ class MyTest { void test() { myObject.wait(); + verify(myObject).wait(); myObject.wait(1L); myObject.wait(2L); - verify(myObject).wait(); verify(myObject, times(2)).wait(anyLong()); } } From 530a6215b94cce8614e49c979dbc211de9533bfc Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 02:57:12 +1000 Subject: [PATCH 05/13] Add support for multiple statements in Verifications to be migrated by replacing the Verification block --- .../java/testing/jmockit/JMockitBlockRewriter.java | 9 ++++++++- .../jmockit/JMockitVerificationsToMockitoTest.java | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 47b1bc3c5..cbe237b7b 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -26,6 +26,8 @@ import java.util.ArrayList; import java.util.List; +import static org.openrewrite.java.testing.jmockit.JMockitBlockType.Verifications; + class JMockitBlockRewriter { private static final String WHEN_TEMPLATE_PREFIX = "when(#{any()})."; @@ -200,14 +202,19 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); JavaCoordinates verifyCoordinates; - if (this.blockType == JMockitBlockType.Verifications) { + if (this.blockType == Verifications) { // for Verifications, replace the Verifications block verifyCoordinates = nextStatementCoordinates; + numStatementsAdded++; } else { // for Expectations put the verify at the end of the method verifyCoordinates = methodBody.getCoordinates().lastStatement(); } + methodBody = rewriteTemplate(verifyTemplate, templateParams, verifyCoordinates); + if (this.blockType == Verifications) { + nextStatementCoordinates = this.methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1).getCoordinates().after(); + } } private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index 0819adde5..e7492a700 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -726,7 +726,7 @@ void test() { } @Test - void whenMultipleVerifications() { + void whenMultipleVerificationsAndMultipleStatements() { //language=java rewriteRun( java( @@ -745,6 +745,7 @@ void test() { myObject.wait(); new Verifications() {{ myObject.wait(); + myObject.wait(anyLong, anyInt); }}; myObject.wait(1L); myObject.wait(2L); @@ -770,6 +771,7 @@ class MyTest { void test() { myObject.wait(); verify(myObject).wait(); + verify(myObject).wait(anyLong(), anyInt()); myObject.wait(1L); myObject.wait(2L); verify(myObject, times(2)).wait(anyLong()); From e705292f21bec6c90828accfe7d8fe1786f77ced Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 02:59:26 +1000 Subject: [PATCH 06/13] Add test case with blank lines in Verification block to be erased --- .../testing/jmockit/JMockitVerificationsToMockitoTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index e7492a700..b0e16c01e 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -743,8 +743,10 @@ class MyTest { void test() { myObject.wait(); - new Verifications() {{ + new Verifications() {{ + myObject.wait(); + myObject.wait(anyLong, anyInt); }}; myObject.wait(1L); From ab1182beee8c77dbc73be953a06dc9f577518059 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 03:03:31 +1000 Subject: [PATCH 07/13] Remove trailing whitespaces in text blocks --- .../JMockitExpectationsToMockitoTest.java | 2 +- .../JMockitVerificationsToMockitoTest.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 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 9712a1e19..b934947f1 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -1375,7 +1375,7 @@ void test() { myObject.wait(1L); myObject.wait(); verify(myObject).wait(anyLong()); - verify(myObject).wait(); + verify(myObject).wait(); } } """ diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index b0e16c01e..12dd4d7bd 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -60,7 +60,7 @@ class MyTest { Object myObject; void test() { - myObject.wait(10L, 10); + myObject.wait(10L, 10); new Verifications() {{ myObject.wait(anyLong, anyInt); }}; @@ -110,7 +110,7 @@ void test() { myObject.wait(10L, 10); new Verifications() {{ myObject.wait(); - }}; + }}; } } """, @@ -176,7 +176,7 @@ void test() { new Verifications() {{ myObject.getSomeField((List) any); myObject.getSomeOtherField((Object) any); - }}; + }}; } } """, @@ -295,7 +295,7 @@ void test() { myObject.getSomeField("foo", "bar", bazz, 10L); new Verifications() {{ myObject.getSomeField("foo", anyString, bazz, 10L); - }}; + }}; } } """, @@ -360,7 +360,7 @@ void test() { myObject.getSomeField("foo"); myObject.getString(); new Verifications() {{ - myObject.getSomeField(anyString); + myObject.getSomeField(anyString); myObject.getString(); }}; } @@ -380,10 +380,10 @@ class MyTest { void test() { String a = "a"; - String s = "s"; + String s = "s"; myObject.getSomeField("foo"); myObject.getString(); - verify(myObject).getSomeField(anyString()); + verify(myObject).getSomeField(anyString()); verify(myObject).getString(); } } @@ -418,11 +418,11 @@ class MyTest { MyObject myObject; void test() { - String a = "a"; + String a = "a"; myObject.getSomeField("foo"); new Verifications() {{ myObject.getSomeField(anyString); - }}; + }}; } } """, @@ -474,7 +474,7 @@ void test() { new Verifications() {{ myObject.wait(anyLong, anyInt); times = 3; - }}; + }}; } } """, @@ -690,7 +690,7 @@ void test() { myOtherObject.getSomeObjectField(); myObject.wait(anyLong, anyInt); myOtherObject.getSomeStringField(anyString, anyLong); - }}; + }}; } } """, @@ -743,7 +743,7 @@ class MyTest { void test() { myObject.wait(); - new Verifications() {{ + new Verifications() {{ myObject.wait(); From a38cb11d9b3f69adbcf2726cdc1eca395c98f998 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Fri, 28 Jun 2024 03:13:10 +1000 Subject: [PATCH 08/13] Refactor --- .../testing/jmockit/JMockitBlockRewriter.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index cbe237b7b..3d7a9a7c7 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -160,11 +160,7 @@ private void rewriteJMockitResult(List results, J.MethodInvocation i methodBody = rewriteTemplate(template, templateParams, nextStatementCoordinates); numStatementsAdded++; - - // the next statement coordinates are directly after the most recently written statement, and subtracting the - // removed jmockit block - nextStatementCoordinates = methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1) - .getCoordinates().after(); + setNextCoordinatesAfterLastStatementAdded(); } private void removeBlock() { @@ -178,7 +174,7 @@ private void removeBlock() { if (bodyStatementIndex == 0) { nextStatementCoordinates = methodBody.getCoordinates().firstStatement(); } else { - nextStatementCoordinates = methodBody.getStatements().get(bodyStatementIndex - 1).getCoordinates().after(); + setNextCoordinatesAfterLastStatementAdded(); } } @@ -205,7 +201,6 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex if (this.blockType == Verifications) { // for Verifications, replace the Verifications block verifyCoordinates = nextStatementCoordinates; - numStatementsAdded++; } else { // for Expectations put the verify at the end of the method verifyCoordinates = methodBody.getCoordinates().lastStatement(); @@ -213,10 +208,17 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex methodBody = rewriteTemplate(verifyTemplate, templateParams, verifyCoordinates); if (this.blockType == Verifications) { - nextStatementCoordinates = this.methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1).getCoordinates().after(); + numStatementsAdded++; + setNextCoordinatesAfterLastStatementAdded(); } } + private void setNextCoordinatesAfterLastStatementAdded() { + // the next statement coordinates are directly after the most recently written statement, calculated by + // subtracting the removed jmockit block + this.nextStatementCoordinates = this.methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1).getCoordinates().after(); + } + private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { return JavaTemplate.builder(verifyTemplate) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) From 1016bf9d17b1afe849a8bf20a111738c6494f90c Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Sun, 30 Jun 2024 21:17:33 +1000 Subject: [PATCH 09/13] Refactor method and variable names. One test case failing when we use an external class to mock an object --- .../testing/jmockit/JMockitBlockRewriter.java | 111 +++++---- .../jmockit/JMockitBlockToMockito.java | 6 +- .../jmockit/SetupStatementsRewriter.java | 2 +- .../JMockitVerificationsToMockitoTest.java | 220 +++++++----------- 4 files changed, 153 insertions(+), 186 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 3d7a9a7c7..2a19884f9 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -35,6 +35,7 @@ class JMockitBlockRewriter { private static final String THROW_TEMPLATE_PREFIX = "thenThrow("; private static final String LITERAL_TEMPLATE_FIELD = "#{}"; private static final String ANY_TEMPLATE_FIELD = "#{any()}"; + private static final String MOCKITO_IMPORT_FQN_PREFX = "org.mockito.Mockito"; private static String getObjectTemplateField(String fqn) { return "#{any(" + fqn + ")}"; @@ -49,10 +50,10 @@ private static String getObjectTemplateField(String fqn) { private J.Block methodBody; private JavaCoordinates nextStatementCoordinates; - private boolean expectationsRewriteFailed = false; + private boolean rewriteFailed = false; - boolean isExpectationsRewriteFailed() { - return expectationsRewriteFailed; + boolean isRewriteFailed() { + return rewriteFailed; } // keep track of the additional statements being added to the method body, which impacts the statement indices @@ -117,52 +118,34 @@ private void rewriteMethodInvocation(List statementsToRewrite) { final MockInvocationResults mockInvocationResults = buildMockInvocationResults(statementsToRewrite); if (mockInvocationResults == null) { // invalid block, cannot rewrite - expectationsRewriteFailed = true; + rewriteFailed = true; return; } J.MethodInvocation invocation = (J.MethodInvocation) statementsToRewrite.get(0); boolean hasResults = !mockInvocationResults.getResults().isEmpty(); if (hasResults) { - rewriteJMockitResult(mockInvocationResults.getResults(), invocation); + rewriteResult(invocation, mockInvocationResults.getResults()); } boolean hasTimes = false; if (mockInvocationResults.getTimes() != null) { hasTimes = true; - writeMethodVerification(invocation, mockInvocationResults.getTimes(), "times"); + rewriteVerify(invocation, mockInvocationResults.getTimes(), "times"); } if (mockInvocationResults.getMinTimes() != null) { hasTimes = true; - writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); + rewriteVerify(invocation, mockInvocationResults.getMinTimes(), "atLeast"); } if (mockInvocationResults.getMaxTimes() != null) { hasTimes = true; - writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); + rewriteVerify(invocation, mockInvocationResults.getMaxTimes(), "atMost"); } if (!hasResults && !hasTimes) { - writeMethodVerification(invocation, null, null); + rewriteVerify(invocation, null, null); } } - private void rewriteJMockitResult(List results, J.MethodInvocation invocation) { - String template = getMockitoStatementTemplate(results); - if (template == null) { - // invalid template, cannot rewrite - expectationsRewriteFailed = true; - return; - } - visitor.maybeAddImport("org.mockito.Mockito", "when"); - - List templateParams = new ArrayList<>(); - templateParams.add(invocation); - templateParams.addAll(results); - - methodBody = rewriteTemplate(template, templateParams, nextStatementCoordinates); - numStatementsAdded++; - setNextCoordinatesAfterLastStatementAdded(); - } - private void removeBlock() { methodBody = JavaTemplate.builder("") .javaParser(JavaParser.fromJavaVersion()) @@ -174,19 +157,31 @@ private void removeBlock() { if (bodyStatementIndex == 0) { nextStatementCoordinates = methodBody.getCoordinates().firstStatement(); } else { - setNextCoordinatesAfterLastStatementAdded(); + setNextCoordinatesAfterLastStatementAdded(0); } } - 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 + private void rewriteResult(J.MethodInvocation invocation, List results) { + String template = getWhenTemplate(results); + if (template == null) { + // invalid template, cannot rewrite + rewriteFailed = true; return; } - visitor.maybeAddImport("org.mockito.Mockito", "verify"); - if (verificationMode != null) { - visitor.maybeAddImport("org.mockito.Mockito", verificationMode); + visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "when"); + + List templateParams = new ArrayList<>(); + templateParams.add(invocation); + templateParams.addAll(results); + + methodBody = rewriteTemplate(template, templateParams, nextStatementCoordinates); + setNextCoordinatesAfterLastStatementAdded(++numStatementsAdded); + } + + private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression times, @Nullable String verificationMode) { + if (invocation.getSelect() == null) { + // cannot write a verification statement for an invocation without a select field + return; } List templateParams = new ArrayList<>(); @@ -195,8 +190,13 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex templateParams.add(times); } templateParams.add(invocation.getName().getSimpleName()); + String verifyTemplate = getVerifyTemplate(invocation.getArguments(), verificationMode, templateParams); + if (verifyTemplate == null) { + // invalid template, cannot rewrite + rewriteFailed = true; + return; + } - String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); JavaCoordinates verifyCoordinates; if (this.blockType == Verifications) { // for Verifications, replace the Verifications block @@ -208,21 +208,34 @@ private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Ex methodBody = rewriteTemplate(verifyTemplate, templateParams, verifyCoordinates); if (this.blockType == Verifications) { - numStatementsAdded++; - setNextCoordinatesAfterLastStatementAdded(); + setNextCoordinatesAfterLastStatementAdded(++numStatementsAdded); + } + + // do this last making sure rewrite worked and specify hasReference=false because in verify case it cannot find + // the static reference in AddImport class, and getSelect() returns not null + visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "verify", false); + if (verificationMode != null) { + visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, verificationMode); } } - private void setNextCoordinatesAfterLastStatementAdded() { + private void setNextCoordinatesAfterLastStatementAdded(int numStatementsAdded) { // the next statement coordinates are directly after the most recently written statement, calculated by // subtracting the removed jmockit block - this.nextStatementCoordinates = this.methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded - 1).getCoordinates().after(); + int nextStatementIdx = bodyStatementIndex + numStatementsAdded - 1; + if (nextStatementIdx >= this.methodBody.getStatements().size()) { + rewriteFailed = true; + } else { + this.nextStatementCoordinates = this.methodBody.getStatements().get(nextStatementIdx).getCoordinates().after(); + } } - private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates rewriteCoords) { - return JavaTemplate.builder(verifyTemplate) + private J.Block rewriteTemplate(String verifyTemplate, List templateParams, JavaCoordinates + rewriteCoords) { + JavaTemplate.Builder builder = JavaTemplate.builder(verifyTemplate) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) - .staticImports("org.mockito.Mockito.*") + .staticImports("org.mockito.Mockito.*"); + return builder .build() .apply( new Cursor(visitor.getCursor(), methodBody), @@ -231,7 +244,7 @@ private J.Block rewriteTemplate(String verifyTemplate, List templatePara ); } - private static String getMockitoStatementTemplate(List results) { + private static String getWhenTemplate(List results) { boolean buildingResults = false; final StringBuilder templateBuilder = new StringBuilder(WHEN_TEMPLATE_PREFIX); for (Expression result : results) { @@ -263,7 +276,8 @@ private static String getMockitoStatementTemplate(List results) { return templateBuilder.toString(); } - private static void appendToTemplate(StringBuilder templateBuilder, boolean buildingResults, String templatePrefix, + private static void appendToTemplate(StringBuilder templateBuilder, boolean buildingResults, String + templatePrefix, String templateField) { if (!buildingResults) { templateBuilder.append(templatePrefix); @@ -273,8 +287,9 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil templateBuilder.append(templateField); } - private static String getVerifyTemplate(List arguments, String fqn, @Nullable String verificationMode, List templateParams) { - StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}"); // verify(object + private static String getVerifyTemplate(List arguments, @Nullable String + verificationMode, List templateParams) { + StringBuilder templateBuilder = new StringBuilder("verify(#{any()}"); // eg verify(object if (verificationMode != null) { templateBuilder.append(", ").append(verificationMode).append("(#{any(int)})"); // eg verify(object, times(2) } @@ -292,7 +307,7 @@ private static String getVerifyTemplate(List arguments, String fqn, } else if (argument instanceof J.Literal) { templateBuilder.append(((J.Literal) argument).getValueSource()); } else { - templateBuilder.append("#{any()}"); + templateBuilder.append(ANY_TEMPLATE_FIELD); templateParams.add(argument); } hasArgument = true; diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java index 4ea93dcfe..4283eedf5 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockToMockito.java @@ -70,12 +70,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl Statement s = statements.get(bodyStatementIndex); Optional blockType = JMockitUtils.getJMockitBlock(s); if (blockType.isPresent()) { - JMockitBlockRewriter ebr = new JMockitBlockRewriter(this, ctx, methodBody, + JMockitBlockRewriter blockRewriter = new JMockitBlockRewriter(this, ctx, methodBody, ((J.NewClass) s), bodyStatementIndex, blockType.get()); - methodBody = ebr.rewriteMethodBody(); + methodBody = blockRewriter.rewriteMethodBody(); statements = methodBody.getStatements(); // if the expectations rewrite failed, skip the next statement - if (ebr.isExpectationsRewriteFailed()) { + if (blockRewriter.isRewriteFailed()) { bodyStatementIndex++; } } else { diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java index ed348c1f9..fbbc1add3 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java @@ -36,7 +36,7 @@ class SetupStatementsRewriter { J.Block rewriteMethodBody() { List statements = methodBody.getStatements(); - // iterate over each statement in the method body, find Expectations blocks and rewrite them + // iterate over each statement in the method body, find JMockit blocks and rewrite them for (Statement s : statements) { if (!JMockitUtils.getJMockitBlock(s).isPresent()) { continue; diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index 12dd4d7bd..e35a506a6 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -138,44 +138,30 @@ void test() { @Test - void whenClassArgumentMatcher() { + void whenTimesNoArgs() { //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.Mocked; import mockit.Verifications; + import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked - MyObject myObject; + Object myObject; void test() { - myObject.getSomeField(new ArrayList<>()); - myObject.getSomeOtherField(new Object()); + myObject.wait(); + myObject.wait(); new Verifications() {{ - myObject.getSomeField((List) any); - myObject.getSomeOtherField((Object) any); + myObject.wait(); + times = 2; }}; } } @@ -184,6 +170,8 @@ void test() { import java.util.ArrayList; import java.util.List; + import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -191,13 +179,12 @@ void test() { @ExtendWith(MockitoExtension.class) class MyTest { @Mock - MyObject myObject; + Object myObject; void test() { - myObject.getSomeField(new ArrayList<>()); - myObject.getSomeOtherField(new Object()); - verify(myObject).getSomeField(anyList()); - verify(myObject).getSomeOtherField(any(Object.class)); + myObject.wait(); + myObject.wait(); + verify(myObject, times(2)).wait(); } } """ @@ -206,30 +193,44 @@ void test() { } @Test - void whenTimesNoArgs() { + 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.Verifications; import mockit.Mocked; + import mockit.Verifications; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) - class MyTest { + class MyTest { @Mocked - Object myObject; + MyObject myObject; void test() { - myObject.wait(); - myObject.wait(); + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); new Verifications() {{ - myObject.wait(); - times = 2; + myObject.getSomeField((List) any); + myObject.getSomeOtherField((Object) any); }}; } } @@ -238,21 +239,22 @@ void test() { import java.util.ArrayList; import java.util.List; - import static org.mockito.Mockito.times; - import static org.mockito.Mockito.verify; + import static org.mockito.Mockito.*; + import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock - Object myObject; + MyObject myObject; void test() { - myObject.wait(); - myObject.wait(); - verify(myObject, times(2)).wait(); + myObject.getSomeField(new ArrayList<>()); + myObject.getSomeOtherField(new Object()); + verify(myObject).getSomeField(anyList()); + verify(myObject).getSomeOtherField(any(Object.class)); } } """ @@ -260,21 +262,11 @@ void test() { ); } + @Test void whenMixedArgumentMatcher() { //language=java rewriteRun( - java( - """ - import java.util.List; - - class MyObject { - public String getSomeField(String s, String s2, String s3, long l1) { - return "X"; - } - } - """ - ), java( """ import java.util.ArrayList; @@ -288,13 +280,12 @@ public String getSomeField(String s, String s2, String s3, long l1) { @ExtendWith(JMockitExtension.class) class MyTest { @Mocked - MyObject myObject; + Object myObject; void test() { - String bazz = "bazz"; - myObject.getSomeField("foo", "bar", bazz, 10L); + myObject.wait(10L, 10); new Verifications() {{ - myObject.getSomeField("foo", anyString, bazz, 10L); + myObject.wait(anyLong, 10); }}; } } @@ -303,21 +294,20 @@ void test() { import java.util.ArrayList; import java.util.List; + import static org.mockito.Mockito.*; + 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 - MyObject myObject; + Object myObject; void test() { - String bazz = "bazz"; - myObject.getSomeField("foo", "bar", bazz, 10L); - verify(myObject).getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L)); + myObject.wait(10L, 10); + verify(myObject).wait(anyLong(), eq(10)); } } """ @@ -329,19 +319,6 @@ void test() { void whenSetupStatements() { //language=java rewriteRun( - java( - """ - class MyObject { - - public String getSomeField(String s) { - return "X"; - } - public String getString() { - return "Y"; - } - } - """ - ), java( """ import mockit.Verifications; @@ -352,16 +329,16 @@ public String getString() { @ExtendWith(JMockitExtension.class) class MyTest { @Mocked - MyObject myObject; + Object myObject; void test() { String a = "a"; String s = "s"; - myObject.getSomeField("foo"); - myObject.getString(); + myObject.wait(1L); + myObject.wait(); new Verifications() {{ - myObject.getSomeField(anyString); - myObject.getString(); + myObject.wait(anyLong); + myObject.wait(); }}; } } @@ -371,20 +348,21 @@ void test() { import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - import static org.mockito.Mockito.*; + import static org.mockito.Mockito.anyLong; + import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) class MyTest { @Mock - MyObject myObject; + Object myObject; void test() { String a = "a"; String s = "s"; - myObject.getSomeField("foo"); - myObject.getString(); - verify(myObject).getSomeField(anyString()); - verify(myObject).getString(); + myObject.wait(1L); + myObject.wait(); + verify(myObject).wait(anyLong()); + verify(myObject).wait(); } } """ @@ -396,15 +374,6 @@ void test() { void whenSetupStatements2() { //language=java rewriteRun( - java( - """ - class MyObject { - public String getSomeField(String s) { - return "X"; - } - } - """ - ), java( """ import mockit.Verifications; @@ -415,13 +384,13 @@ public String getSomeField(String s) { @ExtendWith(JMockitExtension.class) class MyTest { @Mocked - MyObject myObject; + Object myObject; void test() { String a = "a"; - myObject.getSomeField("foo"); + myObject.wait(1L); new Verifications() {{ - myObject.getSomeField(anyString); + myObject.wait(anyLong); }}; } } @@ -431,19 +400,18 @@ void test() { 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.anyLong; import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) class MyTest { @Mock - MyObject myObject; + Object myObject; void test() { String a = "a"; - myObject.getSomeField("foo"); - verify(myObject).getSomeField(anyString()); + myObject.wait(1L); + verify(myObject).wait(anyLong()); } } """ @@ -649,47 +617,31 @@ void test() { void whenMultipleStatements() { //language=java rewriteRun( - java( - """ - class MyObject { - public String getSomeStringField(String input, long otherInput) { - return "X"; - } - public int getSomeIntField() { - return 0; - } - public Object getSomeObjectField() { - return new Object(); - } - public void doSomething() {} - } - """ - ), java( """ import mockit.Verifications; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked Object myObject; @Mocked - MyObject myOtherObject; + Object myOtherObject; void test() { myObject.hashCode(); - myOtherObject.getSomeObjectField(); + myOtherObject.wait(); myObject.wait(10L, 10); - myOtherObject.getSomeStringField("bar", 10L); + myOtherObject.wait(10L); new Verifications() {{ myObject.hashCode(); - myOtherObject.getSomeObjectField(); + myOtherObject.wait(); myObject.wait(anyLong, anyInt); - myOtherObject.getSomeStringField(anyString, anyLong); + myOtherObject.wait(anyLong); }}; } } @@ -698,7 +650,7 @@ 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) @@ -707,17 +659,17 @@ class MyTest { Object myObject; @Mock - MyObject myOtherObject; + Object myOtherObject; void test() { myObject.hashCode(); - myOtherObject.getSomeObjectField(); + myOtherObject.wait(); myObject.wait(10L, 10); - myOtherObject.getSomeStringField("bar", 10L); + myOtherObject.wait(10L); verify(myObject).hashCode(); - verify(myOtherObject).getSomeObjectField(); + verify(myOtherObject).wait(); verify(myObject).wait(anyLong(), anyInt()); - verify(myOtherObject).getSomeStringField(anyString(), anyLong()); + verify(myOtherObject).wait(anyLong()); } } """ From 1ce8b7714b5939a3d6dfb60d2afa39d3ca7fa3dc Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Sun, 30 Jun 2024 23:32:44 +1000 Subject: [PATCH 10/13] Try to debug issue where seeing very strange behaviour in code, where when we change the expected output to the actual generated output by including the static import, we get error that the type is missing or malformed. This looks like a bug in the framework. It seems to only occur when we add a second class in the before. --- .../testing/jmockit/JMockitVerificationsToMockitoTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index e35a506a6..4b37490e3 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -238,9 +238,7 @@ void test() { """ import java.util.ArrayList; import java.util.List; - - import static org.mockito.Mockito.*; - + import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; From 0c237541c078642a7567b5ba0c34644611a2afe5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 1 Jul 2024 23:09:20 +0200 Subject: [PATCH 11/13] Skip type validation after template `verify(#{any()}).#{}(#{any()})` --- .../JMockitExpectationsToMockitoTest.java | 200 +++++++++--------- .../JMockitVerificationsToMockitoTest.java | 100 ++++----- 2 files changed, 152 insertions(+), 148 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 b934947f1..54d26e64c 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -71,7 +71,7 @@ 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) @@ -118,7 +118,7 @@ void test() { 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) @@ -155,7 +155,7 @@ public String getSomeField() { 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) @@ -176,7 +176,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.assertNull; import static org.mockito.Mockito.when; @@ -214,14 +214,14 @@ public int getSomeField() { 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; @@ -280,14 +280,14 @@ public String getSomeField(String s) { 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; @@ -340,16 +340,16 @@ public String getSomeField() { 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()); @@ -403,14 +403,14 @@ public Object getSomeField() { 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; @@ -518,7 +518,7 @@ public String getSomeField() { 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) @@ -567,7 +567,7 @@ void whenClassArgumentMatcher() { java( """ import java.util.List; - + class MyObject { public String getSomeField(List input) { return "X"; @@ -582,19 +582,19 @@ public String getSomeOtherField(Object input) { """ 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((List) any); @@ -610,19 +610,19 @@ void test() { """ 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.*; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + void test() { when(myObject.getSomeField(anyList())).thenReturn(null); when(myObject.getSomeOtherField(any(Object.class))).thenReturn(null); @@ -642,7 +642,7 @@ void whenNoArguments() { java( """ import java.util.List; - + class MyObject { public String getSomeField() { return "X"; @@ -654,19 +654,19 @@ public String getSomeField() { """ 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(); @@ -679,19 +679,19 @@ void test() { """ 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()); @@ -709,7 +709,7 @@ void whenMixedArgumentMatcher() { java( """ import java.util.List; - + class MyObject { public String getSomeField(String s, String s2, String s3, long l1) { return "X"; @@ -721,19 +721,19 @@ public String getSomeField(String s, String s2, String s3, long l1) { """ 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() { String bazz = "bazz"; new Expectations() {{ @@ -747,19 +747,19 @@ void test() { """ 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.*; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + void test() { String bazz = "bazz"; when(myObject.getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L))).thenReturn(null); @@ -778,7 +778,7 @@ void whenSetupStatements() { java( """ class MyObject { - + public String getSomeField(String s) { return "X"; } @@ -794,26 +794,26 @@ public String getString() { 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; @@ -838,7 +838,7 @@ void test() { String s = "s"; when(myObject.getSomeField(anyString())).thenReturn(s); when(myObject.getString()).thenReturn(a); - + assertEquals("s", myObject.getSomeField("foo")); assertEquals("a", myObject.getString()); } @@ -867,14 +867,14 @@ public String getSomeField(String s) { 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() {{ @@ -883,7 +883,7 @@ void test() { String b = "b"; result = s; }}; - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -892,7 +892,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; @@ -907,7 +907,7 @@ void test() { String s = "s"; String b = "b"; when(myObject.getSomeField(anyString())).thenReturn(s); - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -926,12 +926,12 @@ void whenTimes() { 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); @@ -947,14 +947,14 @@ 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); myObject.wait(10L, 10); @@ -972,12 +972,12 @@ 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 @@ -996,9 +996,9 @@ 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 @@ -1019,12 +1019,12 @@ 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 @@ -1043,9 +1043,9 @@ 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 @@ -1066,12 +1066,12 @@ 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 @@ -1091,9 +1091,9 @@ 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 @@ -1129,14 +1129,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(); @@ -1150,15 +1150,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()); @@ -1195,18 +1195,18 @@ public void doSomething() {} 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 Object myObject; - + @Mocked MyObject myOtherObject; - + void test() { new Expectations() {{ myObject.hashCode(); @@ -1276,15 +1276,15 @@ public String getSomeStringField() { 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() {{ myObject.getSomeStringField(); @@ -1335,15 +1335,15 @@ void whenMultipleExpectationsNoResults() { 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 Object myObject; - + void test() { new Expectations() {{ myObject.wait(anyLong); diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java index 4b37490e3..23022dcb5 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsToMockitoTest.java @@ -21,6 +21,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -71,7 +72,7 @@ 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) @@ -118,7 +119,7 @@ void test() { 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) @@ -145,17 +146,17 @@ void whenTimesNoArgs() { """ import java.util.ArrayList; import java.util.List; - + import mockit.Verifications; 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() { myObject.wait(); myObject.wait(); @@ -169,18 +170,18 @@ void test() { """ import java.util.ArrayList; import java.util.List; - + import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock Object myObject; - + void test() { myObject.wait(); myObject.wait(); @@ -196,10 +197,11 @@ void test() { void whenClassArgumentMatcher() { //language=java rewriteRun( + spec -> spec.afterTypeValidationOptions(TypeValidation.builder().methodInvocations(false).build()), java( """ import java.util.List; - + class MyObject { public String getSomeField(List input) { return "X"; @@ -214,17 +216,17 @@ public String getSomeOtherField(Object input) { """ import java.util.ArrayList; import java.util.List; - + import mockit.Mocked; import mockit.Verifications; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) - class MyTest { + class MyTest { @Mocked MyObject myObject; - + void test() { myObject.getSomeField(new ArrayList<>()); myObject.getSomeOtherField(new Object()); @@ -238,16 +240,18 @@ void test() { """ import java.util.ArrayList; import java.util.List; - + + import static org.mockito.Mockito.*; + 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() { myObject.getSomeField(new ArrayList<>()); myObject.getSomeOtherField(new Object()); @@ -269,17 +273,17 @@ void whenMixedArgumentMatcher() { """ import java.util.ArrayList; import java.util.List; - + import mockit.Verifications; 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() { myObject.wait(10L, 10); new Verifications() {{ @@ -291,9 +295,9 @@ void test() { """ import java.util.ArrayList; import java.util.List; - + import static org.mockito.Mockito.*; - + import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -302,7 +306,7 @@ void test() { class MyTest { @Mock Object myObject; - + void test() { myObject.wait(10L, 10); verify(myObject).wait(anyLong(), eq(10)); @@ -323,12 +327,12 @@ void whenSetupStatements() { 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() { String a = "a"; String s = "s"; @@ -345,7 +349,7 @@ void test() { 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.verify; @@ -383,7 +387,7 @@ void whenSetupStatements2() { class MyTest { @Mocked Object myObject; - + void test() { String a = "a"; myObject.wait(1L); @@ -397,7 +401,7 @@ void test() { 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.verify; @@ -427,12 +431,12 @@ void whenTimes() { 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() { myObject.wait(10L, 10); myObject.wait(10L, 10); @@ -448,14 +452,14 @@ 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); myObject.wait(10L, 10); @@ -478,7 +482,7 @@ void whenMinTimes() { import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked @@ -497,9 +501,9 @@ 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 @@ -525,7 +529,7 @@ void whenMaxTimes() { import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked @@ -544,9 +548,9 @@ 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 @@ -572,7 +576,7 @@ void whenMinTimesMaxTimes() { import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked @@ -592,9 +596,9 @@ 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 @@ -626,10 +630,10 @@ void whenMultipleStatements() { class MyTest { @Mocked Object myObject; - + @Mocked Object myOtherObject; - + void test() { myObject.hashCode(); myOtherObject.wait(); @@ -648,7 +652,7 @@ 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) @@ -685,18 +689,18 @@ void whenMultipleVerificationsAndMultipleStatements() { 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() { myObject.wait(); new Verifications() {{ - + myObject.wait(); - + myObject.wait(anyLong, anyInt); }}; myObject.wait(1L); From 328a2e3dc6805f34b6daa01818c777a1691898bf Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 2 Jul 2024 12:00:18 +0200 Subject: [PATCH 12/13] Add missing nullable annotations; add Lombok annotations --- .../testing/jmockit/JMockitBlockRewriter.java | 65 ++++--------------- .../testing/jmockit/JMockitBlockType.java | 16 ++--- 2 files changed, 19 insertions(+), 62 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index 2a19884f9..e561d8bcf 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -15,6 +15,9 @@ */ package org.openrewrite.java.testing.jmockit; +import lombok.AccessLevel; +import lombok.Data; +import lombok.Setter; import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; import org.openrewrite.internal.lang.Nullable; @@ -191,11 +194,6 @@ private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression t } templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), verificationMode, templateParams); - if (verifyTemplate == null) { - // invalid template, cannot rewrite - rewriteFailed = true; - return; - } JavaCoordinates verifyCoordinates; if (this.blockType == Verifications) { @@ -244,7 +242,7 @@ private J.Block rewriteTemplate(String verifyTemplate, List templatePara ); } - private static String getWhenTemplate(List results) { + private static @Nullable String getWhenTemplate(List results) { boolean buildingResults = false; final StringBuilder templateBuilder = new StringBuilder(WHEN_TEMPLATE_PREFIX); for (Expression result : results) { @@ -320,7 +318,7 @@ private static String getVerifyTemplate(List arguments, @Nullable St return templateBuilder.toString(); } - private static MockInvocationResults buildMockInvocationResults(List expectationStatements) { + private static @Nullable MockInvocationResults buildMockInvocationResults(List expectationStatements) { final MockInvocationResults resultWrapper = new MockInvocationResults(); for (int i = 1; i < expectationStatements.size(); i++) { Statement expectationStatement = expectationStatements.get(i); @@ -356,20 +354,19 @@ private static MockInvocationResults buildMockInvocationResults(List return resultWrapper; } - private static String getVariableNameFromAssignment(J.Assignment assignment) { - String name = null; + private static @Nullable String getVariableNameFromAssignment(J.Assignment assignment) { if (assignment.getVariable() instanceof J.Identifier) { - name = ((J.Identifier) assignment.getVariable()).getSimpleName(); + return ((J.Identifier) assignment.getVariable()).getSimpleName(); } else if (assignment.getVariable() instanceof J.FieldAccess) { J.FieldAccess fieldAccess = (J.FieldAccess) assignment.getVariable(); if (fieldAccess.getTarget() instanceof J.Identifier) { - name = fieldAccess.getSimpleName(); + return fieldAccess.getSimpleName(); } } - return name; + return null; } - private static String getPrimitiveTemplateField(JavaType.Primitive primitiveType) { + private static @Nullable String getPrimitiveTemplateField(JavaType.Primitive primitiveType) { switch (primitiveType) { case Boolean: return "#{any(boolean)}"; @@ -396,54 +393,16 @@ private static String getPrimitiveTemplateField(JavaType.Primitive primitiveType } } - private static String getInvocationSelectFullyQualifiedClassName(J.MethodInvocation invocation) { - Expression select = invocation.getSelect(); - if (select == null || select.getType() == null) { - return null; - } - String fqn = null; - if (select.getType() instanceof JavaType.FullyQualified) { - fqn = ((JavaType.FullyQualified) select.getType()).getFullyQualifiedName(); - } - return fqn; - } - + @Data private static class MockInvocationResults { + @Setter(AccessLevel.NONE) private final List results = new ArrayList<>(); private Expression times; private Expression minTimes; private Expression maxTimes; - private List getResults() { - return results; - } - private void addResult(Expression result) { results.add(result); } - - private Expression getTimes() { - return times; - } - - private void setTimes(Expression times) { - this.times = times; - } - - private Expression getMinTimes() { - return minTimes; - } - - private void setMinTimes(Expression minTimes) { - this.minTimes = minTimes; - } - - private Expression getMaxTimes() { - return maxTimes; - } - - private void setMaxTimes(Expression maxTimes) { - this.maxTimes = maxTimes; - } } } diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java index 12cd0b634..e649448a0 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockType.java @@ -15,17 +15,15 @@ */ package org.openrewrite.java.testing.jmockit; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter enum JMockitBlockType { - Expectations("mockit.Expectations"), Verifications("mockit.Verifications"); // Add NonStrictExpectations later + Expectations("mockit.Expectations"), + Verifications("mockit.Verifications"); // Add NonStrictExpectations later private final String fqn; - - JMockitBlockType(String fqn) { - this.fqn = fqn; - } - - String getFqn() { - return fqn; - } } From be42e84677121c48d31eebfbfd0f0fd3f71bdb56 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 2 Jul 2024 13:20:05 +0200 Subject: [PATCH 13/13] Apply suggestions from code review --- .../openrewrite/java/testing/jmockit/JMockitBlockRewriter.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java index e561d8bcf..147bcdd50 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java @@ -274,8 +274,7 @@ private J.Block rewriteTemplate(String verifyTemplate, List templatePara return templateBuilder.toString(); } - private static void appendToTemplate(StringBuilder templateBuilder, boolean buildingResults, String - templatePrefix, + private static void appendToTemplate(StringBuilder templateBuilder, boolean buildingResults, String templatePrefix, String templateField) { if (!buildingResults) { templateBuilder.append(templatePrefix);