Skip to content

Commit

Permalink
Add recipe to mockito recipes
Browse files Browse the repository at this point in the history
Add missing imports
Fill out name and desc
  • Loading branch information
Laurens-W committed Sep 13, 2024
1 parent a99e5d6 commit e690282
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public class MockitoWhenOnStaticToMockStatic extends Recipe {

@Override
public String getDisplayName() {
return "";
return "Replace `Mockito.when` on static (non mock) with try-with-resource with MockedStatic";
}

@Override
public String getDescription() {
return ".";
return "Replace `Mockito.when` on static (non mock) with try-with-resource with MockedStatic as Mockito4 no longer allows this.";
}

@Override
Expand All @@ -60,16 +60,19 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
J.MethodInvocation when = (J.MethodInvocation) ((J.MethodInvocation) stmt).getSelect();
if (when.getArguments().get(0) instanceof J.MethodInvocation && ((J.MethodInvocation) when.getArguments().get(0)).getMethodType().getFlags().contains(Flag.Static)) {
JavaType.FullyQualified arg_fq = TypeUtils.asFullyQualified(when.getArguments().get(0).getType());
String template = String.format("try(MockedStatic<%s> mock%s = mockStatic(%s.class)){\n" +
"mock%s.when(%s::%s).thenReturn(%s);\n" +
"}", arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), ((J.MethodInvocation) when.getArguments().get(0)).getSimpleName(), ((J.MethodInvocation) stmt).getArguments().get(0));
J.Identifier ident = (J.Identifier) ((J.MethodInvocation)when.getArguments().get(0)).getSelect();
String template = String.format("try(MockedStatic<#{}> mock%s = mockStatic(#{}.class)){\n" +
" mock%s.when(#{any()}).thenReturn(#{any()});\n" +
"}", arg_fq.getClassName(), arg_fq.getClassName());
m = JavaTemplate.builder(template)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.imports("org.mockito.MockedStatic")
.staticImports("org.mockito.Mockito.mockStatic")
.build()
.apply(getCursor(), stmt.getCoordinates().replace());
.apply(getCursor(), stmt.getCoordinates().replace(), ident.getType(), ident.getType(), when.getArguments().get(0), ((J.MethodInvocation) stmt).getArguments().get(0));
rewrittenWhen = true;
maybeAddImport("org.mockito.MockedStatic", false);
maybeAddImport("org.mockito.Mockito", "mockStatic");
continue;
}
Expand Down
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 @@ -59,6 +59,7 @@ recipeList:
groupId: org.mockito
artifactId: "*"
newVersion: 4.x
- org.openrewrite.java.testing.mockito.MockitoWhenOnStaticToMockStatic
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.Mockito1to3Migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void test() {
""",
"""
import a.b.A;
import org.mockito.MockedStatic;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
Expand All @@ -87,8 +88,8 @@ class Test {
private A aMock = mock(A.class);
void test() {
try (MockedStatic<A> mockA = mockStatic(A.class)) {
mockA.when(A::getA).thenReturn(aMock);
try (MockedStatic<a.b.A> mockA = mockStatic(a.b.A.class)) {
mockA.when(A.getA()).thenReturn(aMock);
assertEquals(A.getA(), aMock);
}
}
Expand Down

0 comments on commit e690282

Please sign in to comment.