From dd2992ce6cbaf8abc6980125113fcb1ef82bb5bc Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 13 Dec 2024 23:58:43 +0100 Subject: [PATCH] Replace `Mockito.times(0)` with `never()` and remove `Mockito.times(1)` Fixes #561 --- .../mockito/RemoveTimesZeroAndOne.java | 83 +++++++++++++ .../resources/META-INF/rewrite/mockito.yml | 1 + .../mockito/RemoveTimesZeroAndOneTest.java | 117 ++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOne.java create mode 100644 src/test/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOneTest.java diff --git a/src/main/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOne.java b/src/main/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOne.java new file mode 100644 index 000000000..1634f6390 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOne.java @@ -0,0 +1,83 @@ +/* + * 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.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; + +public class RemoveTimesZeroAndOne extends Recipe { + @Override + public String getDisplayName() { + return "Remove `Mockito.times(0)` and `Mockito.times(1)`"; + } + + @Override + public String getDescription() { + return "Remove `Mockito.times(0)` and `Mockito.times(0)` from `Mockito.verify()` calls."; + } + + private static final MethodMatcher verifyMatcher = new MethodMatcher("org.mockito.Mockito verify(..)", false); + private static final MethodMatcher timesMatcher = new MethodMatcher("org.mockito.Mockito times(int)", false); + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + Preconditions.and( + new UsesMethod<>(verifyMatcher), + new UsesMethod<>(timesMatcher) + ), + new JavaIsoVisitor() { + @Override + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); + if (timesMatcher.matches(mi) && J.Literal.isLiteralValue(mi.getArguments().get(0), 0)) { + maybeAddImport("org.mockito.Mockito", "never"); + maybeRemoveImport("org.mockito.Mockito.times"); + return JavaTemplate.builder("never()") + .staticImports("org.mockito.Mockito.never") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core")) + .build() + .apply(getCursor(), mi.getCoordinates().replace()); + } + if (verifyMatcher.matches(mi) && mi.getArguments().size() == 2) { + J.MethodInvocation times = (J.MethodInvocation) mi.getArguments().get(1); + if (timesMatcher.matches(times) && J.Literal.isLiteralValue(times.getArguments().get(0), 1)) { + maybeRemoveImport("org.mockito.Mockito.times"); + JavaType.Method methodType = mi.getMethodType() + .withParameterNames(mi.getMethodType().getParameterNames().subList(0, 1)) + .withParameterTypes(mi.getMethodType().getParameterTypes().subList(0, 1)); + return mi + .withArguments(mi.getArguments().subList(0, 1)) + .withMethodType(methodType) + .withName(mi.getName().withType(methodType)); + } + } + return mi; + } + } + ); + } +} diff --git a/src/main/resources/META-INF/rewrite/mockito.yml b/src/main/resources/META-INF/rewrite/mockito.yml index 7386b8936..0a45cdfa1 100644 --- a/src/main/resources/META-INF/rewrite/mockito.yml +++ b/src/main/resources/META-INF/rewrite/mockito.yml @@ -25,6 +25,7 @@ recipeList: - org.openrewrite.java.testing.mockito.Mockito1to5Migration - org.openrewrite.java.RemoveAnnotation: annotationPattern: "@org.mockito.junit.jupiter.MockitoSettings(strictness=org.mockito.quality.Strictness.WARN)" + - org.openrewrite.java.testing.mockito.RemoveTimesZeroAndOne - org.openrewrite.java.testing.mockito.SimplifyMockitoVerifyWhenGiven --- type: specs.openrewrite.org/v1beta/recipe diff --git a/src/test/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOneTest.java b/src/test/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOneTest.java new file mode 100644 index 000000000..4c8ec6897 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOneTest.java @@ -0,0 +1,117 @@ +/* + * 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.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; + +class RemoveTimesZeroAndOneTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "mockito-core")) + .recipe(new RemoveTimesZeroAndOne()); + } + + + @DocumentExample + @Test + void replaceTimesZero() { + rewriteRun( + //language=Java + java( + """ + import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; + + class MyTest { + void test(Object myObject) { + myObject.wait(); + verify(myObject, times(0)).wait(); + } + } + """, + """ + import static org.mockito.Mockito.never; + import static org.mockito.Mockito.verify; + + class MyTest { + void test(Object myObject) { + myObject.wait(); + verify(myObject, never()).wait(); + } + } + """ + ) + ); + } + + @Test + void removeTimesOne() { + rewriteRun( + //language=Java + java( + """ + import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; + + class MyTest { + void test(Object myObject) { + myObject.wait(); + verify(myObject, times(1)).wait(); + } + } + """, + """ + import static org.mockito.Mockito.verify; + + class MyTest { + void test(Object myObject) { + myObject.wait(); + verify(myObject).wait(); + } + } + """ + ) + ); + } + + @Test + void retainTimesTwo() { + rewriteRun( + //language=Java + java( + """ + import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; + + class MyTest { + void test(Object myObject) { + myObject.wait(); + verify(myObject, times(2)).wait(); + } + } + """ + ) + ); + } +}