-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
Mockito.times(0)
with never()
and remove Mockito.times(1)
Fixes #561
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOne.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/test/java/org/openrewrite/java/testing/mockito/RemoveTimesZeroAndOneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |