Skip to content
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

Add JMockit's MockUp to Mockito migration #599

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b31f293
Add JMockit's MockUp to Mockito migration
SiBorea Sep 4, 2024
1da4931
Adopt mockito-core:5.+ through parserClasspath
timtebeek Sep 11, 2024
4e42b59
Apply bot code suggestions
timtebeek Sep 11, 2024
faa1bb1
Add type check for new class statement
SiBorea Sep 11, 2024
6d92ade
Merge branch 'mockup' of https://github.com/SiBorea/rewrite-testing-f…
SiBorea Sep 11, 2024
96688de
Fix statement construction
SiBorea Sep 11, 2024
f9d9fa1
Remove debug statement
SiBorea Sep 11, 2024
694f1f0
Fix code format
SiBorea Sep 12, 2024
78a4aae
Use CallRealMethod for non mock methods
SiBorea Sep 13, 2024
cda1e64
Update src/test/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
4589bb3
Update src/test/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
c90fb67
Update src/test/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
7c7fecc
Update src/test/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
8a71eb8
Update src/test/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
f9827b0
Update src/main/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
34589a0
Update src/main/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
e03719a
Update src/main/java/org/openrewrite/java/testing/jmockit/JMockitMock…
SiBorea Sep 13, 2024
6c9d156
Add description about private methods
SiBorea Sep 18, 2024
3fd7446
Merge branch 'openrewrite:main' into mockup
SiBorea Sep 18, 2024
162352f
Update JMockitMockUpToMockitoTest.java
timtebeek Sep 18, 2024
a852a60
Fix UT to match gralde build order
SiBorea Sep 19, 2024
c9f8280
Merge branch 'openrewrite:main' into mockup
SiBorea Sep 21, 2024
4ef5811
Add setUp/tearDown support
SiBorea Sep 22, 2024
ac83863
Add MockUp params support
SiBorea Sep 22, 2024
e41c20d
Remove useless
SiBorea Sep 22, 2024
08f3f60
Fix JavaTemplate type inference
SiBorea Sep 23, 2024
8992a87
Remove unused local variables
SiBorea Sep 24, 2024
dce23c8
MockUp to try-with-resource
SiBorea Sep 24, 2024
e691a7b
Merge branch 'main' into mockup
SiBorea Sep 24, 2024
4eeae91
Add import class test
SiBorea Sep 26, 2024
ee06b6e
Add multiple mockUp test
SiBorea Sep 26, 2024
f06d8b0
Merge branch 'mockup' of github.com:SiBorea/rewrite-testing-framework…
SiBorea Sep 26, 2024
a9fc1bc
Apply bot suggestions
SiBorea Sep 27, 2024
9f814cb
Merge branch 'openrewrite:main' into mockup
SiBorea Oct 6, 2024
f1de48c
Remove useless type cast
SiBorea Oct 6, 2024
f987e59
Fix variable naming
SiBorea Oct 6, 2024
28485bb
Remove fqn from naming
SiBorea Oct 6, 2024
fc25bbd
Remove unneeded method call
SiBorea Oct 6, 2024
199f8f7
Shorter var naming
SiBorea Oct 6, 2024
fa85adb
Update JMockitMockUpToMockitoTest.java
timtebeek Oct 6, 2024
8391a98
Merge branch 'main' into mockup
timtebeek Oct 11, 2024
b61496d
Refactor mockup migration to resuse existing code and cleanup
shivanisky Oct 13, 2024
d9eb4a8
Merge branch 'openrewrite:main' into mockup
SiBorea Oct 15, 2024
0dc4d10
Resolve deprecated print()
SiBorea Oct 15, 2024
1e5b465
Utilize LambdaBlockToExpression
SiBorea Oct 15, 2024
b4f1e0a
Make constants
SiBorea Oct 15, 2024
ad0c387
Merge branch 'main' into mockup
shivanisky Oct 22, 2024
750a45b
Apply bot suggestions
SiBorea Oct 24, 2024
b5b1c08
Merge branch 'openrewrite:main' into mockup
SiBorea Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ recipeDependencies {
parserClasspath("com.github.tomakehurst:wiremock-jre8:2.35.0")
parserClasspath("org.mockito:mockito-all:1.10.19")
parserClasspath("org.mockito:mockito-core:3.+")
parserClasspath("org.mockito:mockito-core:5.+")
parserClasspath("org.jmockit:jmockit:1.49")
parserClasspath("org.jmockit:jmockit:1.22") // last version with NonStrictExpectations
parserClasspath("org.mockito:mockito-junit-jupiter:3.+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@Getter
enum JMockitBlockType {

MockUp,
Expectations,
Verifications,
NonStrictExpectations;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2024 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.mockito;
SiBorea marked this conversation as resolved.
Show resolved Hide resolved

import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Statement;

import java.util.List;
import java.util.stream.Collectors;

public class MockitoUtils {
public static J.ClassDeclaration maybeAddMethodWithAnnotation(
JavaVisitor visitor,
J.ClassDeclaration classDecl,
ExecutionContext ctx,
String methodName,
String methodAnnotationSignature,
String methodAnnotationToAdd,
String additionalClasspathResource,
String importToAdd,
String methodAnnotationParameters
) {
if (hasMethodWithAnnotation(classDecl, new AnnotationMatcher(methodAnnotationSignature))) {
return classDecl;
}

J.MethodDeclaration firstTestMethod = getFirstTestMethod(
classDecl.getBody().getStatements().stream().filter(J.MethodDeclaration.class::isInstance)
.map(J.MethodDeclaration.class::cast).collect(Collectors.toList()));

visitor.maybeAddImport(importToAdd);
return JavaTemplate.builder(methodAnnotationToAdd + methodAnnotationParameters + " public void " + methodName + "() {}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, additionalClasspathResource))
.imports(importToAdd)
.build()
.apply(
new Cursor(visitor.getCursor().getParentOrThrow(), classDecl),
(firstTestMethod != null) ?
firstTestMethod.getCoordinates().before() :
classDecl.getBody().getCoordinates().lastStatement()
);
}

private static boolean hasMethodWithAnnotation(J.ClassDeclaration classDecl, AnnotationMatcher annotationMatcher) {
for (Statement statement : classDecl.getBody().getStatements()) {
if (statement instanceof J.MethodDeclaration) {
J.MethodDeclaration methodDeclaration = (J.MethodDeclaration) statement;
List<J.Annotation> allAnnotations = methodDeclaration.getAllAnnotations();
for (J.Annotation annotation : allAnnotations) {
if (annotationMatcher.matches(annotation)) {
return true;
}
}
}
}
return false;
}

private static J.@Nullable MethodDeclaration getFirstTestMethod(List<J.MethodDeclaration> methods) {
for (J.MethodDeclaration methodDeclaration : methods) {
for (J.Annotation annotation : methodDeclaration.getLeadingAnnotations()) {
if ("Test".equals(annotation.getSimpleName())) {
return methodDeclaration;
}
}
}
return null;
}
}

Large diffs are not rendered by default.

Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/jmockit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tags:
- jmockit
recipeList:
- org.openrewrite.java.testing.jmockit.JMockitBlockToMockito
- org.openrewrite.java.testing.jmockit.JMockitMockUpToMockito
- org.openrewrite.java.testing.jmockit.JMockitAnnotatedArgumentToMockito
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: mockit.Mocked
Expand Down
Loading
Loading