Links
Comment on page

Mockito 3.x migration from 1.x

org.openrewrite.java.testing.mockito.Mockito1to3Migration
Upgrade Mockito from 1.x to 3.x.

Tags

  • testing
  • mockito

Recipe source

  • groupId: org.openrewrite.recipe
  • artifactId: rewrite-testing-frameworks
  • version: 2.1.0
This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-testing-frameworks:2.1.0 in your build file or by running a shell command (in which case no build changes are needed):
Gradle
Gradle init script
Maven POM
Maven Command Line
Moderne CLI
  1. 1.
    Add the following to your build.gradle file:
build.gradle
plugins {
id("org.openrewrite.rewrite") version("6.5.6")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.mockito.Mockito1to3Migration")
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.1.0")
}
  1. 2.
    Run gradle rewriteRun to run the recipe.
  1. 1.
    Create a file named init.gradle in the root of your project.
init.gradle
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:6.5.6") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.1.0")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.mockito.Mockito1to3Migration")
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
  1. 2.
    Run gradle --init-script init.gradle rewriteRun to run the recipe.
  1. 1.
    Add the following to your pom.xml file:
pom.xml
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.13.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.testing.mockito.Mockito1to3Migration</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
  1. 2.
    Run mvn rewrite:run to run the recipe.
shell
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:RELEASE \
-Drewrite.activeRecipes=org.openrewrite.java.testing.mockito.Mockito1to3Migration
You will need to have configured the Moderne CLI on your machine before you can run the following command.
shell
mod run . --recipe Mockito1to3Migration

Definition

Recipe List
Yaml Recipe List
    • oldFullyQualifiedTypeName: org.mockito.MockitoAnnotations.Mock
    • newFullyQualifiedTypeName: org.mockito.Mock
    • oldFullyQualifiedTypeName: org.mockito.Matchers
    • newFullyQualifiedTypeName: org.mockito.ArgumentMatchers
    • methodPattern: org.mockito.ArgumentMatchers anyVararg()
    • newMethodName: any
    • methodPattern: org.mockito.ArgumentMatchers anyObject()
    • newMethodName: any
    • methodPattern: org.mockito.ArgumentMatchers anyListOf(java.lang.Class)
    • newMethodName: anyList
    • methodPattern: org.mockito.ArgumentMatchers anyList(java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.ArgumentMatchers anySetOf(java.lang.Class)
    • newMethodName: anySet
    • methodPattern: org.mockito.ArgumentMatchers anySet(java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.ArgumentMatchers anyMapOf(java.lang.Class, java.lang.Class)
    • newMethodName: anyMap
    • methodPattern: org.mockito.ArgumentMatchers anyMap(java.lang.Class, java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.ArgumentMatchers anyMap(java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.ArgumentMatchers anyCollectionOf()
    • newMethodName: anyCollection
    • methodPattern: org.mockito.ArgumentMatchers anyIterableOf()
    • newMethodName: anyIterable
    • methodPattern: org.mockito.ArgumentMatchers isNull(java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.ArgumentMatchers notNull(java.lang.Class)
    • argumentIndex: 0
    • methodPattern: org.mockito.MockedStatic verify(org.mockito.verification.VerificationMode, org.mockito.MockedStatic.Verification)
    • newParameterNames: [verification, mode]
    • oldParameterNames: [mode, verification]
    • methodPattern: org.mockito.Mockito verifyZeroInteractions(..)
    • newMethodName: verifyNoInteractions
    • methodPattern: org.mockito.invocation.InvocationOnMock getArgumentAt(int, java.lang.Class)
    • newMethodName: getArgument
    • oldFullyQualifiedTypeName: org.mockito.exceptions.verification.TooLittleActualInvocations
    • newFullyQualifiedTypeName: org.mockito.exceptions.verification.TooFewActualInvocations
    • oldFullyQualifiedTypeName: org.mockito.configuration.AnnotationEngine
    • newFullyQualifiedTypeName: org.mockito.plugins.AnnotationEngine
    • oldFullyQualifiedTypeName: org.mockito.plugins.InstantiatorProvider
    • newFullyQualifiedTypeName: org.mockito.plugins.InstantiatorProvider2
    • oldFullyQualifiedTypeName: org.mockito.runners.MockitoJUnitRunner
    • newFullyQualifiedTypeName: org.mockito.junit.MockitoJUnitRunner
    • groupId: org.mockito
    • artifactId: mockito-junit-jupiter
    • version: 3.x
    • onlyIfUsing: org.mockito.junit.jupiter.MockitoExtension
    • acceptTransitive: true
    • groupId: org.mockito
    • artifactId: *
    • newVersion: 3.x
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.Mockito1to3Migration
displayName: Mockito 3.x migration from 1.x
description: Upgrade Mockito from 1.x to 3.x.
tags:
- testing
- mockito
recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.MockitoAnnotations.Mock
newFullyQualifiedTypeName: org.mockito.Mock
- org.openrewrite.java.testing.mockito.AnyToNullable
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.Matchers
newFullyQualifiedTypeName: org.mockito.ArgumentMatchers
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyVararg()
newMethodName: any
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyObject()
newMethodName: any
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyListOf(java.lang.Class)
newMethodName: anyList
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers anyList(java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anySetOf(java.lang.Class)
newMethodName: anySet
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers anySet(java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyMapOf(java.lang.Class, java.lang.Class)
newMethodName: anyMap
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers anyMap(java.lang.Class, java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers anyMap(java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyCollectionOf()
newMethodName: anyCollection
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers anyIterableOf()
newMethodName: anyIterable
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers isNull(java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: org.mockito.ArgumentMatchers notNull(java.lang.Class)
argumentIndex: 0
- org.openrewrite.java.ReorderMethodArguments:
methodPattern: org.mockito.MockedStatic verify(org.mockito.verification.VerificationMode, org.mockito.MockedStatic.Verification)
newParameterNames: [verification, mode]
oldParameterNames: [mode, verification]
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.Mockito verifyZeroInteractions(..)
newMethodName: verifyNoInteractions
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.invocation.InvocationOnMock getArgumentAt(int, java.lang.Class)
newMethodName: getArgument
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.exceptions.verification.TooLittleActualInvocations
newFullyQualifiedTypeName: org.mockito.exceptions.verification.TooFewActualInvocations
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.configuration.AnnotationEngine
newFullyQualifiedTypeName: org.mockito.plugins.AnnotationEngine
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.plugins.InstantiatorProvider
newFullyQualifiedTypeName: org.mockito.plugins.InstantiatorProvider2
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.mockito.runners.MockitoJUnitRunner
newFullyQualifiedTypeName: org.mockito.junit.MockitoJUnitRunner
- org.openrewrite.java.testing.mockito.CleanupMockitoImports
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.mockito
artifactId: mockito-junit-jupiter
version: 3.x
onlyIfUsing: org.mockito.junit.jupiter.MockitoExtension
acceptTransitive: true
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: org.mockito
artifactId: *
newVersion: 3.x

See how this recipe works across multiple open-source repositories

Moderne Link Image
The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.
Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Contributors