-
Notifications
You must be signed in to change notification settings - Fork 105
JMockit to Mockito Migration Recipe - Rewrite Expectations Block #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kunli2
merged 13 commits into
openrewrite:main
from
tinder-dthomson:jmockit-expectations-to-mockito
Oct 19, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
df42b15
Recipe to convert JMockit Expectations block to Mockito.when statement
tinder-dthomson 070f8ff
license format
tinder-dthomson 44ed969
formatting
tinder-dthomson b5984d8
another todo
tinder-dthomson 40aa8ee
create new jmockit package and move files
tinder-dthomson d066b95
rename
tinder-dthomson 19d9bfa
remove recipe from mockito yml
tinder-dthomson d15e6f9
update tag
tinder-dthomson e743d77
pass unit test
kunli2 38acaab
polish
kunli2 cd996a0
polish
kunli2 1eaa2db
fix type issue in template, fix license year
tinder-dthomson 410656e
polish
tinder-dthomson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoWhen.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright 2023 the original author or authors. | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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 lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.Expression; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.Statement; | ||
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class JMockitExpectationsToMockitoWhen extends Recipe { | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Rewrite JMockit Expectations"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Rewrites JMockit `Expectations` to `Mockito.when`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new UsesType<>("mockit.*", false), | ||
| new RewriteExpectationsVisitor()); | ||
| } | ||
|
|
||
| private static class RewriteExpectationsVisitor extends JavaVisitor<ExecutionContext> { | ||
| @Override | ||
| public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) { | ||
| J.NewClass nc = (J.NewClass) super.visitNewClass(newClass, executionContext); | ||
| if (!(nc.getClazz() instanceof J.Identifier)) { | ||
| return nc; | ||
| } | ||
| J.Identifier clazz = (J.Identifier) nc.getClazz(); | ||
| if (!clazz.getSimpleName().equals("Expectations")) { | ||
| return nc; | ||
| } | ||
|
|
||
| // empty Expectations block is considered invalid | ||
| assert nc.getBody() != null : "Expectations block is empty"; | ||
|
|
||
| // prepare the statements for moving | ||
| J.Block innerBlock = (J.Block) nc.getBody().getStatements().get(0); | ||
|
|
||
| // TODO: handle multiple mock statements | ||
| Statement mockInvocation = innerBlock.getStatements().get(0); | ||
| Expression result = ((J.Assignment) innerBlock.getStatements().get(1)).getAssignment(); | ||
|
|
||
| // apply the template and replace the `new Expectations()` statement coordinates | ||
| // TODO: handle exception results with another template | ||
| J.MethodInvocation newMethod = JavaTemplate.builder("when(#{any()}).thenReturn(#{});") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(executionContext, "mockito-core-3.12")) | ||
| .staticImports("org.mockito.Mockito.when") | ||
| .build() | ||
| .apply( | ||
| getCursor(), | ||
| nc.getCoordinates().replace(), | ||
| mockInvocation, | ||
| result | ||
| ); | ||
|
|
||
| // handle import changes | ||
| maybeAddImport("org.mockito.Mockito", "when"); | ||
| maybeRemoveImport("mockit.Expectations"); | ||
|
|
||
| return newMethod.withPrefix(nc.getPrefix()); | ||
| } | ||
| } | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # | ||
| # Copyright 2023 the original author or authors. | ||
| # <p> | ||
| # 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 | ||
| # <p> | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # <p> | ||
| # 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. | ||
| # | ||
| --- | ||
| type: specs.openrewrite.org/v1beta/recipe | ||
| name: org.openrewrite.java.testing.jmockit.JMockitToMockito | ||
| displayName: Migrate from JMockit to Mockito | ||
| description: This recipe will apply changes commonly needed when migrating from JMockit to Mockito. | ||
| tags: | ||
| - testing | ||
| - jmockit | ||
| recipeList: | ||
| - org.openrewrite.java.ChangeType: | ||
| oldFullyQualifiedTypeName: mockit.Mocked | ||
| newFullyQualifiedTypeName: org.mockito.Mock | ||
| - org.openrewrite.java.ChangeType: | ||
| oldFullyQualifiedTypeName: mockit.integration.junit5.JMockitExtension | ||
| newFullyQualifiedTypeName: org.mockito.junit.jupiter.MockitoExtension | ||
| - org.openrewrite.java.testing.jmockit.JMockitExpectationsToMockitoWhen |
103 changes: 103 additions & 0 deletions
103
src/test/java/org/openrewrite/java/testing/jmockit/JMockitToMockitoTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright 2023 the original author or authors. | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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 static org.openrewrite.java.Assertions.java; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.InMemoryExecutionContext; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| class JMockitToMockitoTest 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" | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void jMockitExpectationsToMockitoWhen() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class MyObject { | ||
| public String getSomeField() { | ||
| return "X"; | ||
| } | ||
| } | ||
| """ | ||
| ), | ||
| java( | ||
| """ | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import mockit.Expectations; | ||
| 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() { | ||
| new Expectations() {{ | ||
| myObject.getSomeField(); | ||
| result = null; | ||
| }}; | ||
| assertNull(myObject.getSomeField()); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class MyTest { | ||
| @Mock | ||
| MyObject myObject; | ||
|
|
||
| void test() { | ||
| when(myObject.getSomeField()).thenReturn(null); | ||
| assertNull(myObject.getSomeField()); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.