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 @MockitoSettings(strictness = ...) based on how Mockito is used, not based on version alone #623

Merged
merged 10 commits into from
Oct 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
Expand Down Expand Up @@ -74,6 +75,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public static class MockitoRuleToMockitoExtensionVisitor extends JavaIsoVisitor<ExecutionContext> {
private static final String MOCKITO_RULE_INVOCATION_KEY = "mockitoRuleInvocation";
private static final String MOCKITO_TEST_RULE_INVOCATION_KEY = "mockitoTestRuleInvocation";
private static final String STRICTNESS_KEY = "strictness";

private static final String EXTEND_WITH_MOCKITO_EXTENSION = "@org.junit.jupiter.api.extension.ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)";
private static final String RUN_WITH_MOCKITO_JUNIT_RUNNER = "@org.junit.runner.RunWith(org.mockito.runners.MockitoJUnitRunner.class)";
Expand All @@ -100,6 +102,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
if (classDecl.getBody().getStatements().size() != cd.getBody().getStatements().size() &&
(FindAnnotations.find(classDecl.withBody(null), RUN_WITH_MOCKITO_JUNIT_RUNNER).isEmpty() &&
FindAnnotations.find(classDecl.withBody(null), EXTEND_WITH_MOCKITO_EXTENSION).isEmpty())) {
String strictness = getCursor().pollMessage(STRICTNESS_KEY);

cd = JavaTemplate.builder("@ExtendWith(MockitoExtension.class)")
.javaParser(JavaParser.fromJavaVersion()
Expand All @@ -110,6 +113,23 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex

maybeAddImport("org.junit.jupiter.api.extension.ExtendWith");
maybeAddImport("org.mockito.junit.jupiter.MockitoExtension");

if (strictness != null) {
cd = JavaTemplate.builder("@MockitoSettings(strictness = " + strictness + ")")
.doBeforeParseTemplate(System.out::println)
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5.9", "mockito-junit-jupiter-3.12"))
.imports("org.mockito.junit.jupiter.MockitoSettings", "org.mockito.quality.Strictness")
.build()
.apply(updateCursor(cd), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
maybeAddImport("org.mockito.junit.jupiter.MockitoSettings", false);
maybeAddImport("org.mockito.quality.Strictness", false);
}

// Workaround first modifier incorrectly getting a trailing space as part of the prefix
cd = cd.withModifiers(ListUtils.mapFirst(cd.getModifiers(),
modifier -> modifier.withPrefix(modifier.getPrefix().withWhitespace(
modifier.getPrefix().getLastWhitespace().replaceAll(" $", "")))));
}
}

Expand All @@ -119,10 +139,27 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (method.getMethodType() != null) {
String key = null;
if (TypeUtils.isOfClassType(method.getMethodType().getDeclaringType(), "org.mockito.junit.MockitoRule")) {
getCursor().putMessageOnFirstEnclosing(J.MethodDeclaration.class, MOCKITO_RULE_INVOCATION_KEY, method);
key = MOCKITO_RULE_INVOCATION_KEY;
} else if (TypeUtils.isOfClassType(method.getMethodType().getDeclaringType(), "org.mockito.junit.MockitoTestRule")) {
getCursor().putMessageOnFirstEnclosing(J.MethodDeclaration.class, MOCKITO_TEST_RULE_INVOCATION_KEY, method);
key = MOCKITO_TEST_RULE_INVOCATION_KEY;
}
if (key != null) {
getCursor().putMessageOnFirstEnclosing(J.MethodDeclaration.class, key, method);
String strictness = null;
switch (method.getSimpleName()) {
case "strictness":
strictness = method.getArguments().get(0).toString();
break;
case "silent":
strictness = "Strictness.LENIENT";
break;
}
if (strictness != null && !strictness.contains("STRICT_STUBS")) {
strictness = strictness.startsWith("Strictness.") ? strictness : "Strictness." + strictness;
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, STRICTNESS_KEY, strictness);
}
}
}

Expand Down
Loading