Skip to content

Commit

Permalink
Replace Mockito.times(0) with never() and remove Mockito.times(1)
Browse files Browse the repository at this point in the history
Fixes #561
  • Loading branch information
timtebeek committed Dec 13, 2024
1 parent 8d07aec commit dd2992c
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<ExecutionContext>() {
@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;
}
}
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/mockito.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
"""
)
);
}
}

0 comments on commit dd2992c

Please sign in to comment.