|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.testing.mockito; |
| 17 | + |
| 18 | +import lombok.Getter; |
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Preconditions; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.AnnotationMatcher; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.MethodMatcher; |
| 27 | +import org.openrewrite.java.search.UsesMethod; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.java.tree.Statement; |
| 30 | + |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +public class RemoveDoNothingForDefaultMocks extends Recipe { |
| 35 | + |
| 36 | + @Getter |
| 37 | + final String displayName = "Remove `doNothing()` for void methods on `@Mock` fields"; |
| 38 | + |
| 39 | + @Getter |
| 40 | + final String description = "Remove unnecessary `doNothing()` stubbings for void methods on `@Mock` fields. " + |
| 41 | + "Mockito mocks already do nothing for void methods by default, making these stubbings redundant " + |
| 42 | + "and triggering strict stubbing violations in Mockito 3+."; |
| 43 | + |
| 44 | + private static final MethodMatcher DO_NOTHING_MATCHER = new MethodMatcher("org.mockito.Mockito doNothing()"); |
| 45 | + private static final MethodMatcher STUBBER_WHEN_MATCHER = new MethodMatcher("org.mockito.stubbing.Stubber when(..)"); |
| 46 | + private static final AnnotationMatcher MOCK_ANNOTATION_MATCHER = new AnnotationMatcher("@org.mockito.Mock"); |
| 47 | + |
| 48 | + @Override |
| 49 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 50 | + return Preconditions.check( |
| 51 | + new UsesMethod<>(DO_NOTHING_MATCHER), |
| 52 | + new JavaIsoVisitor<ExecutionContext>() { |
| 53 | + @Override |
| 54 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 55 | + Set<String> mockFieldNames = new HashSet<>(); |
| 56 | + for (Statement stmt : classDecl.getBody().getStatements()) { |
| 57 | + if (stmt instanceof J.VariableDeclarations) { |
| 58 | + J.VariableDeclarations vd = (J.VariableDeclarations) stmt; |
| 59 | + if (vd.getLeadingAnnotations().stream().anyMatch(MOCK_ANNOTATION_MATCHER::matches)) { |
| 60 | + for (J.VariableDeclarations.NamedVariable var : vd.getVariables()) { |
| 61 | + mockFieldNames.add(var.getSimpleName()); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + getCursor().putMessage("mockFieldNames", mockFieldNames); |
| 67 | + return super.visitClassDeclaration(classDecl, ctx); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 72 | + J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); |
| 73 | + if (mi != null && isDoNothingOnMockField(mi)) { |
| 74 | + maybeRemoveImport("org.mockito.Mockito.doNothing"); |
| 75 | + return null; |
| 76 | + } |
| 77 | + return mi; |
| 78 | + } |
| 79 | + |
| 80 | + private boolean isDoNothingOnMockField(J.MethodInvocation mi) { |
| 81 | + // Pattern: doNothing().when(mock).someVoidMethod(args) |
| 82 | + if (!(mi.getSelect() instanceof J.MethodInvocation)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + J.MethodInvocation whenCall = (J.MethodInvocation) mi.getSelect(); |
| 86 | + if (!STUBBER_WHEN_MATCHER.matches(whenCall)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + // Ensure doNothing() is standalone (not chained after doThrow() etc.) |
| 90 | + if (!(whenCall.getSelect() instanceof J.MethodInvocation)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + J.MethodInvocation doNothingCall = (J.MethodInvocation) whenCall.getSelect(); |
| 94 | + if (!DO_NOTHING_MATCHER.matches(doNothingCall) || doNothingCall.getSelect() != null) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + // Check that the when() argument references a @Mock field |
| 98 | + if (whenCall.getArguments().isEmpty() || !(whenCall.getArguments().get(0) instanceof J.Identifier)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + String mockName = ((J.Identifier) whenCall.getArguments().get(0)).getSimpleName(); |
| 102 | + Set<String> mockFieldNames = getCursor().getNearestMessage("mockFieldNames"); |
| 103 | + return mockFieldNames != null && mockFieldNames.contains(mockName); |
| 104 | + } |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments