-
Notifications
You must be signed in to change notification settings - Fork 96
Add @ExtendWith(MockitoExtension.class) Test using Mockito Annotations
#848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
28e9fe3
recipe to add `@ExtendWith(MockitoExtension.class)` to tests iff `@Mo…
MBoegers 4bf1c71
include in greater recipe
MBoegers 9666e27
add documentation annotation
MBoegers 7f01a76
add test to prove inference with existing annotations is not a problem
MBoegers d41a08e
simplify JavaTemplateBuilder usage
MBoegers dc4ed9e
apply code review comments
MBoegers 12d5f8e
fix integration test now having ``@ExtendWith`
MBoegers 5d7d377
widen mockito annotation coverage
MBoegers 07f9c08
prevent migration if no JUnit 5 class is used and fix tests
MBoegers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/org/openrewrite/java/testing/mockito/AddMockitoExtensionIfAnnotationsUsed.java
This file contains hidden or 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,68 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <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.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.search.FindAnnotations; | ||
| import org.openrewrite.java.search.IsLikelyTest; | ||
| import org.openrewrite.java.tree.J; | ||
|
|
||
| import static org.openrewrite.Preconditions.*; | ||
|
|
||
| public class AddMockitoExtensionIfAnnotationsUsed extends Recipe { | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Adds Mockito extensions to Mockito tests"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Adds `@ExtendWith(MockitoExtension.class)` to tests using `@Mock` or `@Captor`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return check(and( | ||
| new IsLikelyTest().getVisitor(), | ||
| not(new FindAnnotations("org.junit.jupiter.api.extension.ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)", false).getVisitor()), | ||
| or(new FindAnnotations("org.mockito.Captor", false).getVisitor(), | ||
| new FindAnnotations("org.mockito.Mock", false).getVisitor())), new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
|
|
||
| maybeAddImport("org.mockito.junit.jupiter.MockitoExtension"); | ||
| maybeAddImport("org.junit.jupiter.api.extension.ExtendWith"); | ||
|
|
||
| J.Annotation extendsWith = ((J.ClassDeclaration) JavaTemplate.builder("@ExtendWith(MockitoExtension.class)") | ||
| .imports("org.mockito.junit.jupiter.MockitoExtension") | ||
| .imports("org.junit.jupiter.api.extension.ExtendWith") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api", "mockito-junit-jupiter")) | ||
| .build() | ||
| .apply(getCursor(), classDecl.getCoordinates().replaceAnnotations())) | ||
Jenson3210 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .getLeadingAnnotations().get(0); | ||
|
|
||
| return autoFormat(classDecl.withLeadingAnnotations(ListUtils.concat(classDecl.getLeadingAnnotations(), extendsWith)), ctx); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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
...t/java/org/openrewrite/java/testing/mockito/AddMockitoExtensionIfAnnotationsUsedTest.java
This file contains hidden or 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 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <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; | ||
MBoegers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 AddMockitoExtensionIfAnnotationsUsedTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new AddMockitoExtensionIfAnnotationsUsed()) | ||
| .parser(JavaParser.fromJavaVersion() | ||
| .classpathFromResources( new InMemoryExecutionContext(),"junit-jupiter-api", "mockito-junit-jupiter", "mockito-core") | ||
| .dependsOn("public class Service {}")); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
MBoegers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void addForMock(){ | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import org.mockito.Mock; | ||
|
|
||
| class Test { | ||
| @Mock | ||
| Service service; | ||
| } | ||
| """, | ||
| """ | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class Test { | ||
| @Mock | ||
| Service service; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void addForCaptor(){ | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import org.mockito.Captor; | ||
|
|
||
| class Test { | ||
| @Captor | ||
| Service service; | ||
| } | ||
| """, | ||
| """ | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Captor; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class Test { | ||
| @Captor | ||
| Service service; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void dontAddIfPresent(){ | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Captor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class Test { | ||
| @Captor | ||
| Service service; | ||
| @Mock | ||
| Service service; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.