diff --git a/src/main/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarn.java b/src/main/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarn.java deleted file mode 100644 index cdda4962a..000000000 --- a/src/main/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarn.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.mockito; - -import org.jspecify.annotations.Nullable; -import org.openrewrite.*; -import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.JavaParser; -import org.openrewrite.java.JavaTemplate; -import org.openrewrite.java.RemoveAnnotation; -import org.openrewrite.java.dependencies.DependencyInsight; -import org.openrewrite.java.search.FindAnnotations; -import org.openrewrite.java.search.UsesType; -import org.openrewrite.java.tree.J; - -import java.util.Comparator; -import java.util.Set; -import java.util.concurrent.atomic.AtomicBoolean; - -public class RetainStrictnessWarn extends ScanningRecipe { - - private static final String EXTEND_WITH_FQ = "org.junit.jupiter.api.extension.ExtendWith"; - private static final String MOCKITO_EXTENSION_FQ = "org.mockito.junit.jupiter.MockitoExtension"; - private static final String MOCKITO_SETTINGS_FQ = "org.mockito.junit.jupiter.MockitoSettings"; - private static final String MOCKITO_STRICTNESS_FQ = "org.mockito.quality.Strictness"; - - private static final String EXTEND_WITH_MOCKITO_EXTENSION = "@" + EXTEND_WITH_FQ + "(" + MOCKITO_EXTENSION_FQ + ".class)"; - - @Override - public String getDisplayName() { - return "Retain Mockito strictness `WARN` when switching to JUnit 5"; - } - - @Override - public String getDescription() { - return "Migrating from JUnit 4 to 5 [changes the default strictness](https://stackoverflow.com/a/53234137/53444) of the mocks from `WARN` to `STRICT_STUBS`. " + - "To prevent tests from failing we restore the original behavior by adding `@MockitoSettings(strictness = Strictness.WARN)`."; - } - - @Override - public AtomicBoolean getInitialValue(ExecutionContext ctx) { - return new AtomicBoolean(false); - } - - @Override - public TreeVisitor getScanner(AtomicBoolean usingOlderMockito) { - TreeVisitor div = new DependencyInsight("org.mockito", "mockito-*", "[1.1,2.17)", null).getVisitor(); - return new TreeVisitor() { - @Override - public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { - if (!usingOlderMockito.get() && div.visit(tree, ctx) != tree) { - usingOlderMockito.set(true); - } - return tree; - } - }; - } - - @Override - public TreeVisitor getVisitor(AtomicBoolean usingOlderMockito) { - return Preconditions.check(usingOlderMockito.get(), - Preconditions.check( - Preconditions.and( - new UsesType<>(MOCKITO_EXTENSION_FQ, true), - Preconditions.not(new UsesType<>(MOCKITO_SETTINGS_FQ, false)) - ), new JavaIsoVisitor() { - @Override - public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { - Set annotations = FindAnnotations.find(classDecl, EXTEND_WITH_MOCKITO_EXTENSION); - if (!annotations.isEmpty()) { - maybeAddImport(MOCKITO_SETTINGS_FQ); - maybeAddImport(MOCKITO_STRICTNESS_FQ); - classDecl = JavaTemplate.builder("@MockitoSettings(strictness = Strictness.WARN)") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-junit-jupiter", "mockito-core")) - .imports(MOCKITO_SETTINGS_FQ, MOCKITO_STRICTNESS_FQ) - .build() - .apply(getCursor(), classDecl.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); - doAfterVisit(new RemoveAnnotation(EXTEND_WITH_MOCKITO_EXTENSION).getVisitor()); - maybeRemoveImport(EXTEND_WITH_FQ); - maybeRemoveImport(MOCKITO_EXTENSION_FQ); - } - return super.visitClassDeclaration(classDecl, ctx); - } - }) - ); - } -} diff --git a/src/main/resources/META-INF/rewrite/mockito.yml b/src/main/resources/META-INF/rewrite/mockito.yml index 5e5c61f6f..d1b62eaf2 100644 --- a/src/main/resources/META-INF/rewrite/mockito.yml +++ b/src/main/resources/META-INF/rewrite/mockito.yml @@ -154,7 +154,6 @@ recipeList: - org.openrewrite.java.testing.mockito.MockUtilsToStatic - org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension - org.openrewrite.java.testing.mockito.ReplacePowerMockito - - org.openrewrite.java.testing.mockito.RetainStrictnessWarn - org.openrewrite.java.dependencies.AddDependency: groupId: org.mockito artifactId: mockito-junit-jupiter diff --git a/src/test/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarnTest.java b/src/test/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarnTest.java deleted file mode 100644 index 1f9fbae59..000000000 --- a/src/test/java/org/openrewrite/java/testing/mockito/RetainStrictnessWarnTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.mockito; - -import org.intellij.lang.annotations.Language; -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; -import static org.openrewrite.maven.Assertions.pomXml; - -class RetainStrictnessWarnTest implements RewriteTest { - - @Language("xml") - private static final String POM_XML_WITH_OLDER_MOCKITO = """ - - 4.0.0 - bla.bla - bla-bla - 1.0.0 - - - org.mockito - mockito-all - 1.1 - test - - - - """; - - @Language("java") - private static final String JAVA_BEFORE = """ - import org.junit.jupiter.api.extension.ExtendWith; - import org.mockito.junit.jupiter.MockitoExtension; - - @ExtendWith(MockitoExtension.class) - class MyTest { - } - """; - - @Language("java") - private static final String JAVA_AFTER = """ - import org.mockito.junit.jupiter.MockitoSettings; - import org.mockito.quality.Strictness; - - @MockitoSettings(strictness = Strictness.WARN) - class MyTest { - } - """; - - @Override - public void defaults(RecipeSpec spec) { - spec - .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), - "mockito-core", "mockito-junit-jupiter", "junit-jupiter-api")) - .recipe(new RetainStrictnessWarn()); - } - - @Test - @DocumentExample - void shouldAddMockitoSettingsWithLenientStubbing() { - //language=java - rewriteRun( - pomXml(POM_XML_WITH_OLDER_MOCKITO), - java(JAVA_BEFORE, JAVA_AFTER) - ); - } - - @Test - void shouldLeaveExisting() { - //language=java - rewriteRun( - pomXml(POM_XML_WITH_OLDER_MOCKITO), - java( - """ - import org.mockito.junit.jupiter.MockitoSettings; - import org.mockito.quality.Strictness; - - @MockitoSettings(strictness = Strictness.STRICT_STUBS) - class MyTest { - } - """ - ) - ); - } - - @Test - void shouldRunBeforeMockitoCore2_17() { - rewriteRun( - pomXml(POM_XML_WITH_OLDER_MOCKITO), - java(JAVA_BEFORE, JAVA_AFTER) - ); - } - - @Test - void shouldNotRunOnNewerMockito() { - rewriteRun( - //language=xml - pomXml( - """ - - 4.0.0 - bla.bla - bla-bla - 1.0.0 - - - org.mockito - mockito-core - 2.17.0 - test - - - - """ - ), - java(JAVA_BEFORE) - ); - } -}