Skip to content

Commit e53bea1

Browse files
committed
Add recipe for migrating MockitoJUnitRunner.Silent runner to MockitoExtension with LENIENT settings. (fixes #222)
1 parent be8d8d6 commit e53bea1

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.mockito;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.java.AnnotationMatcher;
21+
import org.openrewrite.java.JavaIsoVisitor;
22+
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.java.JavaTemplate;
24+
import org.openrewrite.java.search.UsesType;
25+
import org.openrewrite.java.testing.junit5.RunnerToExtension;
26+
import org.openrewrite.java.tree.J;
27+
28+
import java.util.Collections;
29+
import java.util.Comparator;
30+
31+
public class MockitoJUnitRunnerSilentToExtension extends Recipe {
32+
@Override
33+
public String getDisplayName() {
34+
return "JUnit 4 MockitoJUnitRunner.Silent to JUnit Jupiter MockitoExtension with LENIENT settings";
35+
}
36+
37+
@Override
38+
public String getDescription() {
39+
return "Replace `@RunWith(MockitoJUnitRunner.Silent.class)` with `@ExtendWith(MockitoExtension.class)` and `@MockitoSettings(strictness = Strictness.LENIENT)`.";
40+
}
41+
42+
@Override
43+
protected UsesType<ExecutionContext> getSingleSourceApplicableTest() {
44+
return new UsesType<>("org.mockito.junit.MockitoJUnitRunner$Silent");
45+
}
46+
47+
@Override
48+
protected JavaIsoVisitor<ExecutionContext> getVisitor() {
49+
50+
return new JavaIsoVisitor<ExecutionContext>() {
51+
final JavaParser parser = JavaParser.fromJavaVersion().dependsOn(
52+
"package org.mockito.quality;" +
53+
"public enum Strictness {" +
54+
" LENIENT,WARN,STRICT_STUBS;" +
55+
" private Strictness() {}" +
56+
"}",
57+
"package org.mockito.junit.jupiter;" +
58+
"import org.mockito.quality.Strictness;" +
59+
"public @interface MockitoSettings {" +
60+
" Strictness strictness() default Strictness.STRICT_STUBS;" +
61+
"}").build();
62+
final AnnotationMatcher silentRunnerMatcher = new AnnotationMatcher("org.junit.runner.RunWith @RunWith(org.mockito.junit.MockitoJUnitRunner.MockitoJUnitRunner.Silent.class)");
63+
64+
@Override
65+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
66+
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
67+
if (cd.getLeadingAnnotations().stream().anyMatch(silentRunnerMatcher::matches)) {
68+
JavaTemplate template = JavaTemplate.builder(this::getCursor, "@MockitoSettings(strictness = Strictness.LENIENT)")
69+
.imports("org.mockito.quality.Strictness", "org.mockito.junit.jupiter.MockitoSettings")
70+
.javaParser(() -> parser)
71+
.build();
72+
cd = maybeAutoFormat(cd, cd.withTemplate(template, cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))), executionContext);
73+
doAfterVisit(new RunnerToExtension(Collections.singletonList("org.mockito.junit.MockitoJUnitRunner$Silent"),
74+
"org.mockito.junit.jupiter.MockitoExtension"));
75+
maybeRemoveImport("org.mockito.junit.MockitoJUnitRunner");
76+
maybeAddImport("org.mockito.quality.Strictness");
77+
maybeAddImport("org.mockito.junit.jupiter.MockitoSettings");
78+
}
79+
return cd;
80+
}
81+
};
82+
}
83+
}

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ tags:
135135
- mockito
136136
recipeList:
137137
- org.openrewrite.java.testing.mockito.Mockito1to3Migration
138+
- org.openrewrite.java.testing.mockito.MockitoJUnitRunnerSilentToExtension
138139
- org.openrewrite.java.testing.junit5.RunnerToExtension:
139140
runners:
140141
- org.mockito.runners.MockitoJUnitRunner
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.mockito
17+
18+
import org.junit.jupiter.api.Test
19+
import org.openrewrite.Recipe
20+
import org.openrewrite.java.JavaParser
21+
import org.openrewrite.java.JavaRecipeTest
22+
23+
class MockitoJunitRunnerSilentToExtensionTest : JavaRecipeTest {
24+
override val parser: JavaParser = JavaParser.fromJavaVersion()
25+
.classpath("mockito-core", "junit")
26+
.build()
27+
28+
override val recipe: Recipe
29+
get() = MockitoJUnitRunnerSilentToExtension()
30+
31+
@Test
32+
fun migrateMockitoRunnerSilentToExtension() = assertChanged(
33+
before = """
34+
import org.junit.runner.RunWith;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
@RunWith(MockitoJUnitRunner.Silent.class)
38+
public class ExternalAPIServiceTest {
39+
}
40+
""",
41+
after = """
42+
import org.junit.jupiter.api.extension.ExtendWith;
43+
import org.mockito.junit.jupiter.MockitoExtension;
44+
import org.mockito.junit.jupiter.MockitoSettings;
45+
import org.mockito.quality.Strictness;
46+
47+
@MockitoSettings(strictness = Strictness.LENIENT)
48+
@ExtendWith(MockitoExtension.class)
49+
public class ExternalAPIServiceTest {
50+
}
51+
"""
52+
)
53+
}

0 commit comments

Comments
 (0)