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, ExecutionContext> getVisitor() {
+ return Preconditions.check(
+ Preconditions.and(
+ new UsesMethod<>(verifyMatcher),
+ new UsesMethod<>(timesMatcher)
+ ),
+ new JavaIsoVisitor
+ * 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();
+ }
+ }
+ """
+ )
+ );
+ }
+}