diff --git a/src/main/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListener.java b/src/main/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListener.java index d443398ce..63b6e717d 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListener.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListener.java @@ -19,6 +19,7 @@ import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; +import org.openrewrite.Option; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; @@ -28,6 +29,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.FindImports; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.trait.Annotated; import org.openrewrite.java.trait.MethodAccess; @@ -57,6 +59,14 @@ public class ReplaceMockitoTestExecutionListener extends Recipe { private static final AnnotationMatcher AFTER_METHOD_MATCHER = new AnnotationMatcher("@org.testng.annotations.AfterMethod"); + @Option(displayName = "Target framework", + description = "The test framework to use when imports alone cannot determine the framework. " + + "Typically set by wrapper recipes that check project dependencies.", + valid = {"jupiter", "junit4", "testng"}, + required = false) + @Nullable + String targetFramework; + String displayName = "Replace `MockitoTestExecutionListener` with the equivalent Mockito test initialization"; String description = "Replace `@TestExecutionListeners(MockitoTestExecutionListener.class)` with the appropriate " + @@ -64,7 +74,7 @@ public class ReplaceMockitoTestExecutionListener extends Recipe { "`@RunWith(MockitoJUnitRunner.class)` for JUnit 4, or `MockitoAnnotations.openMocks(this)` for TestNG."; private enum TestFramework { - JUNIT5, JUNIT4, TESTNG + JUPITER, JUNIT4, TESTNG } @Override @@ -79,10 +89,10 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat return cd; } - TestFramework framework = detectFramework(cd); + TestFramework framework = detectFramework(cd, ctx); // Skip if replacement already exists or can't be added - if (framework == TestFramework.JUNIT5 && context.extendWithMockitoFound) { + if (framework == TestFramework.JUPITER && context.extendWithMockitoFound) { return cd; } if (framework == TestFramework.JUNIT4 && context.runWithFound) { @@ -90,7 +100,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat } // Add replacement based on framework switch (framework) { - case JUNIT5: + case JUPITER: cd = JavaTemplate.builder("@ExtendWith(MockitoExtension.class)") .imports("org.junit.jupiter.api.extension.ExtendWith", "org.mockito.junit.jupiter.MockitoExtension") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api", "mockito-junit-jupiter")) @@ -186,27 +196,39 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat })); } - private TestFramework detectFramework(J.ClassDeclaration cd) { - // Check extends for TestNG base classes + private TestFramework detectFramework(J.ClassDeclaration cd, ExecutionContext ctx) { + // Structural evidence always wins if (cd.getExtends() != null && TypeUtils.isAssignableTo(ABSTRACT_TESTNG_SPRING, cd.getExtends().getType())) { return TestFramework.TESTNG; } - // Check compilation unit imports + // Import-based detection J.CompilationUnit cu = getCursor().firstEnclosingOrThrow(J.CompilationUnit.class); - for (J.Import imp : cu.getImports()) { - String pkg = imp.getPackageName(); - if (pkg.startsWith("org.junit") && !pkg.startsWith("org.junit.jupiter")) { - return TestFramework.JUNIT4; - } - if (pkg.startsWith("org.testng")) { - return TestFramework.TESTNG; + if (new FindImports("org.junit.jupiter..*", null).getVisitor().visit(cu, ctx) != cu) { + return TestFramework.JUPITER; + } + if (new FindImports("org.junit..*", null).getVisitor().visit(cu, ctx) != cu) { + return TestFramework.JUNIT4; + } + if (new FindImports("org.testng..*", null).getVisitor().visit(cu, ctx) != cu) { + return TestFramework.TESTNG; + } + + // Dependency-based fallback from YAML wrapper recipes + if (targetFramework != null) { + switch (targetFramework) { + case "junit5": + return TestFramework.JUPITER; + case "junit4": + return TestFramework.JUNIT4; + case "testng": + return TestFramework.TESTNG; } } // Default to JUnit 5 - return TestFramework.JUNIT5; + return TestFramework.JUPITER; } }); } diff --git a/src/main/resources/META-INF/rewrite/mockito.yml b/src/main/resources/META-INF/rewrite/mockito.yml index 37f6f7231..55947ccc3 100644 --- a/src/main/resources/META-INF/rewrite/mockito.yml +++ b/src/main/resources/META-INF/rewrite/mockito.yml @@ -180,7 +180,9 @@ recipeList: - org.openrewrite.java.testing.mockito.MockUtilsToStatic - org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension - org.openrewrite.java.testing.mockito.AddMockitoExtensionIfAnnotationsUsed - - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJupiter + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJUnit4 + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForTestNG - org.openrewrite.java.testing.mockito.RemoveInitMocksIfRunnersSpecified - org.openrewrite.java.testing.mockito.ReplacePowerMockito - org.openrewrite.java.dependencies.ChangeDependency: @@ -199,3 +201,45 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: org.mockito.Mockito anyObject() newMethodName: any +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJupiter +displayName: Replace `MockitoTestExecutionListener` (JUnit Jupiter projects) +description: >- + Replace `MockitoTestExecutionListener` in projects that have JUnit Jupiter as a dependency. + Uses `@ExtendWith(MockitoExtension.class)` as the replacement. +preconditions: + - org.openrewrite.java.dependencies.search.ModuleHasDependency: + groupIdPattern: org.junit.jupiter + artifactIdPattern: junit-jupiter* +recipeList: + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener: + targetFramework: jupiter +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJUnit4 +displayName: Replace `MockitoTestExecutionListener` (JUnit 4 projects) +description: >- + Replace `MockitoTestExecutionListener` in projects that have JUnit 4 as a dependency. + Uses `@RunWith(MockitoJUnitRunner.class)` as the replacement. +preconditions: + - org.openrewrite.java.dependencies.search.ModuleHasDependency: + groupIdPattern: junit + artifactIdPattern: junit +recipeList: + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener: + targetFramework: junit4 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForTestNG +displayName: Replace `MockitoTestExecutionListener` (TestNG projects) +description: >- + Replace `MockitoTestExecutionListener` in projects that have TestNG as a dependency. + Uses `MockitoAnnotations.openMocks(this)` with `@BeforeMethod`/`@AfterMethod` as the replacement. +preconditions: + - org.openrewrite.java.dependencies.search.ModuleHasDependency: + groupIdPattern: org.testng + artifactIdPattern: testng* +recipeList: + - org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener: + targetFramework: testng diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv index fdee4d06f..32931f988 100644 --- a/src/main/resources/META-INF/rewrite/recipes.csv +++ b/src/main/resources/META-INF/rewrite/recipes.csv @@ -67,11 +67,11 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.SimplifyRedundantAssertJChains,Simplify redundant AssertJ assertion chains,Removes redundant AssertJ assertions when chained methods already provide the same or stronger guarantees.,1,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.SimplifySequencedCollectionAssertions,Simplify AssertJ assertions on SequencedCollection,"Simplify AssertJ assertions on SequencedCollection by using dedicated assertion methods. For example, `assertThat(sequencedCollection.getLast())` can be simplified to `assertThat(sequencedCollection).last()`.",1,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.SimplifyStreamMapToExtracting,Simplify `assertThat(collection.stream().map(...))` to `assertThat(collection).extracting(...)`,Simplifies AssertJ assertions that use `stream().map()` to extract values from a collection by using the dedicated `extracting()` method instead. This makes the assertion more readable and leverages AssertJ's built-in extraction capabilities.,1,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.Assertj,AssertJ best practices,Migrates JUnit asserts to AssertJ and applies best practices to assertions.,862,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.Assertj,AssertJ best practices,Migrates JUnit asserts to AssertJ and applies best practices to assertions.,867,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.StaticImports,Statically import AssertJ's `assertThat`,Consistently use a static import rather than inlining the `Assertions` class name in tests.,3,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions,Simplify AssertJ chained assertions,Replace AssertJ assertions where a method is called on the actual value with a dedicated assertion.,63,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.SimplifyAssertJAssertions,Shorten AssertJ assertions,Replace AssertJ assertions where a dedicated assertion is available for the same actual value.,9,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.JUnitToAssertj,Migrate JUnit asserts to AssertJ,"AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.junit.jupiter.api.Assertions` to `org.assertj.core.api.Assertions`. Will convert JUnit 4 to JUnit Jupiter if necessary to match and modify assertions.",316,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.JUnitToAssertj,Migrate JUnit asserts to AssertJ,"AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.junit.jupiter.api.Assertions` to `org.assertj.core.api.Assertions`. Will convert JUnit 4 to JUnit Jupiter if necessary to match and modify assertions.",321,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.FestToAssertj,Migrate Fest 2.x to AssertJ,"AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts Fest 2.x imports to AssertJ imports.",10,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsBooleanToAssertBoolean,"Replace JUnit `assertEquals(false, )` to `assertFalse()` / `assertTrue()`",Using `assertFalse` or `assertTrue` is simpler and more clear.,1,Cleanup,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull,"`assertEquals(a, null)` to `assertNull(a)`",Using `assertNull(a)` is simpler and more clear.,1,Cleanup,Testing,Java,,,Basic building blocks for transforming Java code.,, @@ -100,7 +100,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.dbrider.MigrateDbRiderSpringToDbRiderJUnit5,Migrate rider-spring (JUnit4) to rider-junit5 (JUnit5),This recipe will migrate the necessary dependencies and annotations from DbRider with JUnit4 to JUnit5 in a Spring application.,3,DBRider,Testing,Java,Recipes for [DBRider](https://database-rider.github.io/database-rider/) database testing framework.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.easymock.EasyMockVerifyToMockitoVerify,Replace EasyMock `verify` calls with Mockito `verify` calls,Replace `EasyMock.verify(dependency)` with individual `Mockito.verify(dependency).method()` calls based on expected methods.,1,EasyMock,Testing,Java,Recipes for migrating from [EasyMock](https://easymock.org/) to Mockito.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.easymock.RemoveExtendsEasyMockSupport,Migrate Test classes that extend `org.easymock.EasyMockSupport` to use Mockito,Modify test classes by removing extends EasyMockSupport and replacing EasyMock methods with Mockito equivalents.,1,EasyMock,Testing,Java,Recipes for migrating from [EasyMock](https://easymock.org/) to Mockito.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.easymock.EasyMockToMockito,Migrate from EasyMock to Mockito,This recipe will apply changes commonly needed when migrating from EasyMock to Mockito.,163,EasyMock,Testing,Java,Recipes for migrating from [EasyMock](https://easymock.org/) to Mockito.,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.easymock.EasyMockToMockito,Migrate from EasyMock to Mockito,This recipe will apply changes commonly needed when migrating from EasyMock to Mockito.,173,EasyMock,Testing,Java,Recipes for migrating from [EasyMock](https://easymock.org/) to Mockito.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.hamcrest.AssertThatBooleanToAssertJ,"Migrate Hamcrest `assertThat(boolean, Matcher)` to AssertJ","Replace Hamcrest `assertThat(String, boolean)` with AssertJ `assertThat(boolean).as(String).isTrue()`.",1,Hamcrest,Testing,Java,Recipes for migrating from [Hamcrest](http://hamcrest.org/) matchers to AssertJ.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.hamcrest.HamcrestEveryItemToAssertJ,Migrate Hamcrest `everyItem` to AssertJ,Migrate Hamcrest `everyItem` to AssertJ `allSatisfy` or `hasOnlyElementsOfType`.,1,Hamcrest,Testing,Java,Recipes for migrating from [Hamcrest](http://hamcrest.org/) matchers to AssertJ.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.hamcrest.HamcrestHasItemMatcherToAssertJ,Migrate Hamcrest `hasItem(Matcher)` to AssertJ,Migrate Hamcrest `hasItem(Matcher)` to AssertJ `hasAtLeastOneElementOfType` or `anySatisfy`.,1,Hamcrest,Testing,Java,Recipes for migrating from [Hamcrest](http://hamcrest.org/) matchers to AssertJ.,,Basic building blocks for transforming Java code.,, @@ -119,7 +119,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.jmockit.JMockitAnnotatedArgumentToMockito,Convert JMockit `@Mocked` and `@Injectable` annotated arguments,Convert JMockit `@Mocked` and `@Injectable` annotated arguments into Mockito statements.,1,JMockit,Testing,Java,Recipes for migrating from [JMockit](https://jmockit.github.io/) to Mockito.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.jmockit.JMockitBlockToMockito,"Rewrite JMockit Expectations, NonStrictExpectations, Verifications, VerificationsInOrder, FullVerifications","Rewrites JMockit `Expectations, NonStrictExpectations, Verifications, VerificationsInOrder, FullVerifications` blocks to Mockito statements.",1,JMockit,Testing,Java,Recipes for migrating from [JMockit](https://jmockit.github.io/) to Mockito.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.jmockit.JMockitMockUpToMockito,Rewrite JMockit MockUp to Mockito statements,Rewrites JMockit `MockUp` blocks to Mockito statements. This recipe will not rewrite private methods in MockUp.,1,JMockit,Testing,Java,Recipes for migrating from [JMockit](https://jmockit.github.io/) to Mockito.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.jmockit.JMockitToMockito,Migrate from JMockit to Mockito,This recipe will apply changes commonly needed when migrating from JMockit to Mockito.,129,JMockit,Testing,Java,Recipes for migrating from [JMockit](https://jmockit.github.io/) to Mockito.,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.jmockit.JMockitToMockito,Migrate from JMockit to Mockito,This recipe will apply changes commonly needed when migrating from JMockit to Mockito.,139,JMockit,Testing,Java,Recipes for migrating from [JMockit](https://jmockit.github.io/) to Mockito.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.AddMissingNested,JUnit 5 inner test classes should be annotated with `@Nested`,Adds `@Nested` to inner classes that contain JUnit 5 tests.,1,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.AddMissingTestBeforeAfterAnnotations,"Add missing `@BeforeEach`, `@AfterEach`, `@Test` to overriding methods","Adds `@BeforeEach`, `@AfterEach`, `@Test` to methods overriding superclass methods if the annotations are present on the superclass method.",1,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.AddParameterizedTestAnnotation,Add missing `@ParameterizedTest` annotation when `@ValueSource` is used or replace `@Test` with `@ParameterizedTest`,Add missing `@ParameterizedTest` annotation when `@ValueSource` is used or replace `@Test` with `@ParameterizedTest`.,1,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, @@ -160,13 +160,13 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.UseWiremockExtension,Use wiremock extension,"As of 2.31.0, wiremock [supports JUnit 5](https://wiremock.org/docs/junit-jupiter/) via an extension.",2,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.AddHamcrestJUnitDependency,Add Hamcrest JUnit dependency,Add Hamcrest JUnit dependency only if JUnit 4's `assertThat` or `assumeThat` is used.,1,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.AddJupiterDependencies,Add JUnit Jupiter dependencies,"Adds JUnit Jupiter dependencies to a Maven or Gradle project. JUnit Jupiter can be added either with the artifact `junit-jupiter`, or both of `junit-jupiter-api` and `junit-jupiter-engine`. This adds `junit-jupiter` dependency unless `junit-jupiter-api` or `junit-jupiter-engine` are already present.",1,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.JUnit5BestPractices,JUnit 5 best practices,Applies best practices to tests.,234,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.JUnit5BestPractices,JUnit 5 best practices,Applies best practices to tests.,239,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.StaticImports,Statically import JUnit Jupiter assertions,Always use a static import for assertion methods.,2,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.JUnit4to5Migration,JUnit Jupiter migration from JUnit 4.x,Migrates JUnit 4.x tests to JUnit Jupiter.,166,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.JUnit4to5Migration,JUnit Jupiter migration from JUnit 4.x,Migrates JUnit 4.x tests to JUnit Jupiter.,171,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.ExcludeJUnit4UnlessUsingTestcontainers,"Exclude JUnit 4, unless Testcontainers is used","Excludes JUnit 4, as it ought not to be necessary in a JUnit 5 project, unless Testcontainers is used.",2,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.UseHamcrestAssertThat,Use `MatcherAssert#assertThat(..)`,JUnit 4's `Assert#assertThat(..)` This method was deprecated in JUnit 4 and removed in JUnit Jupiter.,3,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.MigrateAssumptions,Use `Assertions#assume*(..)` and Hamcrest's `MatcherAssume#assume*(..)`,Many of JUnit 4's `Assume#assume(..)` methods have no direct counterpart in JUnit 5 and require Hamcrest JUnit's `MatcherAssume`.,7,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.UseMockitoExtension,Use Mockito JUnit Jupiter extension,Migrate uses of `@RunWith(MockitoJUnitRunner.class)` (and similar annotations) to `@ExtendWith(MockitoExtension.class)`.,56,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.UseMockitoExtension,Use Mockito JUnit Jupiter extension,Migrate uses of `@RunWith(MockitoJUnitRunner.class)` (and similar annotations) to `@ExtendWith(MockitoExtension.class)`.,61,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.IgnoreToDisabled,Use JUnit Jupiter `@Disabled`,Migrates JUnit 4.x `@Ignore` to JUnit Jupiter `@Disabled`.,2,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.ThrowingRunnableToExecutable,Use JUnit Jupiter `Executable`,Migrates JUnit 4.x `ThrowingRunnable` to JUnit Jupiter `Executable`.,2,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit5.MigrateAssertionFailedError,Migrate JUnit 4 assertion failure exceptions to JUnit Jupiter,Replace JUnit 4's `junit.framework.AssertionFailedError` and `org.junit.ComparisonFailure` with JUnit Jupiter's `org.opentest4j.AssertionFailedError`.,3,JUnit Jupiter,Testing,Java,Best practices for JUnit Jupiter tests.,,Basic building blocks for transforming Java code.,, @@ -198,16 +198,19 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.RemoveInitMocksIfRunnersSpecified,Remove `MockitoAnnotations.initMocks(this)` and `openMocks(this)` if JUnit runners specified,Remove `MockitoAnnotations.initMocks(this)` and `MockitoAnnotations.openMocks(this)` if class-level JUnit runners `@RunWith(MockitoJUnitRunner.class)` or `@ExtendWith(MockitoExtension.class)` are specified. These manual initialization calls are redundant when using Mockito's JUnit integration.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.RemoveTimesZeroAndOne,Remove `Mockito.times(0)` and `Mockito.times(1)`,Remove `Mockito.times(0)` and `Mockito.times(1)` from `Mockito.verify()` calls.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceInitMockToOpenMock,Replace `MockitoAnnotations.initMocks(this)` to `MockitoAnnotations.openMocks(this)`,Replace `MockitoAnnotations.initMocks(this)` to `MockitoAnnotations.openMocks(this)` and generate `AutoCloseable` mocks.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener,Replace `MockitoTestExecutionListener` with the equivalent Mockito test initialization,"Replace `@TestExecutionListeners(MockitoTestExecutionListener.class)` with the appropriate Mockito initialization for the test framework in use: `@ExtendWith(MockitoExtension.class)` for JUnit 5, `@RunWith(MockitoJUnitRunner.class)` for JUnit 4, or `MockitoAnnotations.openMocks(this)` for TestNG.",1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListener,Replace `MockitoTestExecutionListener` with the equivalent Mockito test initialization,"Replace `@TestExecutionListeners(MockitoTestExecutionListener.class)` with the appropriate Mockito initialization for the test framework in use: `@ExtendWith(MockitoExtension.class)` for JUnit 5, `@RunWith(MockitoJUnitRunner.class)` for JUnit 4, or `MockitoAnnotations.openMocks(this)` for TestNG.",1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,"[{""name"":""targetFramework"",""type"":""String"",""displayName"":""Target framework"",""description"":""The test framework to use when imports alone cannot determine the framework. Valid values: `junit4`, `junit5`, `testng`. Typically set by wrapper recipes that check project dependencies."",""example"":""junit5""}]", maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.SimplifyMockitoVerifyWhenGiven,"Call to Mockito method ""verify"", ""when"" or ""given"" should be simplified","Fixes Sonar issue `java:S6068`: Call to Mockito method ""verify"", ""when"" or ""given"" should be simplified.",1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.AnyToNullable,Replace Mockito 1.x `anyString()`/`any()` with `nullable(Class)`,Since Mockito 2.10 `anyString()` and `any()` no longer matches null values. Use `nullable(Class)` instead.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplacePowerMockDependencies,Replace PowerMock dependencies with Mockito equivalents,"Replaces PowerMock API dependencies with `mockito-inline` when `mockStatic()`, `whenNew()`, or `@PrepareForTest` usage is detected, or `mockito-core` otherwise. PowerMock features like static mocking, constructor mocking, and final class mocking require the inline mock maker which is bundled in `mockito-inline` for Mockito 3.x/4.x.",1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.VerifyZeroToNoMoreInteractions,Replace `verifyZeroInteractions()` with `verifyNoMoreInteractions()`,Replaces `verifyZeroInteractions()` with `verifyNoMoreInteractions()` in Mockito tests when migration when using a Mockito version < 3.x.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.MockitoBestPractices,Mockito best practices,Applies best practices for Mockito tests.,117,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to5Migration,Mockito 5.x upgrade,Upgrade Mockito from 1.x to 5.x.,112,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito4to5Only,Mockito 4 to 5.x upgrade only,Upgrade Mockito from 4.x to 5.x. Does not include 1.x to 4.x migration.,58,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to4Migration,Mockito 4.x upgrade,Upgrade Mockito from 1.x to 4.x.,53,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to3Migration,Mockito 3.x migration from 1.x,Upgrade Mockito from 1.x to 3.x.,48,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.MockitoBestPractices,Mockito best practices,Applies best practices for Mockito tests.,127,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to5Migration,Mockito 5.x upgrade,Upgrade Mockito from 1.x to 5.x.,122,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito4to5Only,Mockito 4 to 5.x upgrade only,Upgrade Mockito from 4.x to 5.x. Does not include 1.x to 4.x migration.,63,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to4Migration,Mockito 4.x upgrade,Upgrade Mockito from 1.x to 4.x.,58,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.Mockito1to3Migration,Mockito 3.x migration from 1.x,Upgrade Mockito from 1.x to 3.x.,53,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJupiter,Replace `MockitoTestExecutionListener` (JUnit 5 projects),Replace `MockitoTestExecutionListener` in projects that have JUnit Jupiter as a dependency. Uses `@ExtendWith(MockitoExtension.class)` as the replacement.,2,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForJUnit4,Replace `MockitoTestExecutionListener` (JUnit 4 projects),Replace `MockitoTestExecutionListener` in projects that have JUnit 4 as a dependency. Uses `@RunWith(MockitoJUnitRunner.class)` as the replacement.,2,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplaceMockitoTestExecutionListenerForTestNG,Replace `MockitoTestExecutionListener` (TestNG projects),Replace `MockitoTestExecutionListener` in projects that have TestNG as a dependency. Uses `MockitoAnnotations.openMocks(this)` with `@BeforeMethod`/`@AfterMethod` as the replacement.,2,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.ReplacePowerMockito,Replace PowerMock with raw Mockito,PowerMockito with raw Mockito; best executed as part of a Mockito upgrade.,13,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.testcontainers.AddTestcontainersAnnotations,Adopt `@Container` and add `@Testcontainers`,Convert Testcontainers `@Rule`/`@ClassRule` to JUnit 5 `@Container` and add `@Testcontainers`.,1,Testcontainers,Testing,Java,Recipes for [Testcontainers](https://testcontainers.com/) integration testing with Docker.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.testcontainers.ConvertToRawType,Remove parameterized type arguments from a Java class,Convert parameterized types of a specified Java class to their raw types.,1,Testcontainers,Testing,Java,Recipes for [Testcontainers](https://testcontainers.com/) integration testing with Docker.,,Basic building blocks for transforming Java code.,"[{""name"":""fullyQualifiedTypeName"",""type"":""String"",""displayName"":""Fully qualified type name"",""description"":""The fully qualified name of the Java class to convert to its raw type."",""example"":""org.testcontainers.containers.PostgreSQLContainer"",""required"":true}]", diff --git a/src/test/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListenerTest.java b/src/test/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListenerTest.java index 18afe3d71..ab096847e 100644 --- a/src/test/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListenerTest.java +++ b/src/test/java/org/openrewrite/java/testing/mockito/ReplaceMockitoTestExecutionListenerTest.java @@ -35,7 +35,7 @@ public void defaults(RecipeSpec spec) { "junit-4.13", "mockito-core-3", "testng-7")) - .recipe(new ReplaceMockitoTestExecutionListener()); + .recipe(new ReplaceMockitoTestExecutionListener(null)); } // --- JUnit 5 tests --- @@ -552,6 +552,87 @@ public void test() {} ); } + // --- targetFramework option tests --- + + @Test + void targetFrameworkFallbackWhenNoImports() { + rewriteRun( + spec -> spec.recipe(new ReplaceMockitoTestExecutionListener("testng")), + //language=java + java( + """ + import org.springframework.test.context.TestExecutionListeners; + import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener; + import org.mockito.Mock; + + @TestExecutionListeners(listeners = {MockitoTestExecutionListener.class}) + public abstract class BaseTest { + @Mock + Object mockService; + } + """, + """ + import org.springframework.test.context.TestExecutionListeners; + import org.testng.annotations.AfterMethod; + import org.testng.annotations.BeforeMethod; + import org.mockito.MockitoAnnotations; + import org.mockito.Mock; + + @TestExecutionListeners + public abstract class BaseTest { + private AutoCloseable mockitoCloseable; + @Mock + Object mockService; + + @BeforeMethod + public void initMocks() { + mockitoCloseable = MockitoAnnotations.openMocks(this); + } + + @AfterMethod + public void closeMocks() throws Exception { + mockitoCloseable.close(); + } + } + """ + ) + ); + } + + @Test + void importDetectionOverridesTargetFramework() { + rewriteRun( + spec -> spec.recipe(new ReplaceMockitoTestExecutionListener("testng")), + //language=java + java( + """ + import org.springframework.test.context.TestExecutionListeners; + import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener; + import org.junit.jupiter.api.Test; + + @TestExecutionListeners(listeners = {MockitoTestExecutionListener.class}) + public class SampleTest { + @Test + void test() {} + } + """, + """ + import org.springframework.test.context.TestExecutionListeners; + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.junit.jupiter.MockitoExtension; + import org.junit.jupiter.api.Test; + + @ExtendWith(MockitoExtension.class) + @TestExecutionListeners + public class SampleTest { + @Test + void test() {} + } + """ + ) + ); + } + // --- Negative tests --- @Test