Skip to content

Commit

Permalink
- Introduce NoInitializationForInjectMock and use it in mockito.yml
Browse files Browse the repository at this point in the history
- Let easymock apply MockitoBestPractices
  • Loading branch information
jevanlingen committed Nov 11, 2024
1 parent 2ec8acc commit 64d3e9b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 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.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.J;

import java.util.stream.Collectors;

public class NoInitializationForInjectMock extends Recipe {

private static final AnnotationMatcher INJECT_MOCKS = new AnnotationMatcher("@org.mockito.InjectMocks");

@Override
public String getDisplayName() {
return "Remove initialization when using @InjectMocks";
}

@Override
public String getDescription() {
return "Removes unnecessary initialization for fields annotated with @InjectMocks in Mockito tests.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.mockito.*", false), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDeclarations, ExecutionContext ctx) {
J.VariableDeclarations vd = super.visitVariableDeclarations(variableDeclarations, ctx);

if (new AnnotationService().matches(getCursor(), INJECT_MOCKS)) {
return vd.withVariables(vd.getVariables().stream().map(var -> var.withInitializer(null)).collect(Collectors.toList()));
}

return vd;
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/easymock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ recipeList:
oldFullyQualifiedTypeName: org.easymock.TestSubject
newFullyQualifiedTypeName: org.mockito.InjectMocks
# - com.todo.openrewrite.java.easymock.RefactorEasyMockIArgumentMatcherIntoMockitoArgumentMatcher
- org.openrewrite.java.testing.mockito.MockitoBestPractices
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.mockito
artifactId: mockito-core
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 @@ -151,6 +151,7 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.runners.MockitoJUnitRunner
newFullyQualifiedTypeName: org.mockito.junit.MockitoJUnitRunner
- org.openrewrite.java.testing.mockito.NoInitializationForInjectMock
- org.openrewrite.java.testing.mockito.CleanupMockitoImports
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 NoInitializationForInjectMockTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "mockito-junit-jupiter-3.12", "mockito-core-3.12"))
.recipe(new NoInitializationForInjectMock());
}

@Test
@DocumentExample
void removeInitializationOfInjectMocks() {
//language=java
rewriteRun(
java(
"""
class MyObject {
private String someField;
public MyObject(String someField) {
this.someField = someField;
}
}
"""
),
java(
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class MyTest {
@InjectMocks
MyObject myObject = new MyObject("someField");
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class MyTest {
@InjectMocks
MyObject myObject;
}
"""
)
);
}
}

0 comments on commit 64d3e9b

Please sign in to comment.