Open source
Mockito 3.x migration from 1.x
Recipe ID
org.openrewrite.java.testing.mockito.Mockito1to3MigrationArtifact
org.openrewrite.recipe:rewrite-testing-frameworksDefinition
Preconditions1
Recipes40
40 recipes
- Change type
- Replace Mockito 1.x
anyString()/any()withnullable(Class) - Change type
- Convert
ArgumentMatcher<T>anonymous class to lambda - Change method name
- Change method name
- Change method name
- Delete method argument
- Change method name
- Delete method argument
- Change method name
- Delete method argument
- Delete method argument
- Change method name
- Change method name
- Delete method argument
- Delete method argument
- Reorder method arguments
- Replace
verifyZeroInteractions()withverifyNoMoreInteractions() - Change method name
- Change method name
- Change type
- Change type
- Change type
- Change type
- Replace undeclared checked exceptions in
thenThrowwithRuntimeException - Cleanup Mockito imports
- Use static form of Mockito
MockUtil - Replace PowerMock with raw Mockito
- JUnit 4
MockitoJUnitto JUnit JupiterMockitoExtension - Add
@MockitoSettings(strictness = Strictness.WARN)for legacy Mockito modules - Replace
MockitoTestExecutionListener(JUnit Jupiter projects) - Replace
MockitoTestExecutionListener(JUnit 4 projects) - Replace
MockitoTestExecutionListener(TestNG projects) - Remove
MockitoAnnotations.initMocks(this)andopenMocks(this)if JUnit runners specified - Remove
doNothing()for void methods on@Mockfields - Change Gradle or Maven dependency
- Upgrade Gradle or Maven dependency versions
- Upgrade Gradle or Maven dependency versions
- Change method name
Examples
groovy, java, xml
- groovy
- Diff
Before
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
testImplementation("org.mockito:mockito-all:1.10.19")
testImplementation("org.mockito:mockito-junit-jupiter:2.28.2")
}
test {
useJUnitPlatform()
}
After
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
testImplementation("org.mockito:mockito-core:3.12.4")
testImplementation("org.mockito:mockito-junit-jupiter:3.12.4")
}
test {
useJUnitPlatform()
}
--- build.gradle
+++ build.gradle
@@ -9,2 +9,2 @@
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
- testImplementation("org.mockito:mockito-all:1.10.19")
- testImplementation("org.mockito:mockito-junit-jupiter:2.28.2")
+ testImplementation("org.mockito:mockito-core:3.12.4")
+ testImplementation("org.mockito:mockito-junit-jupiter:3.12.4")
}
- java
- Diff
Before
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
class MyTest {
@Mock
Object objectMock;
private A subject;
@BeforeEach
void setup() {
subject = new A();
}
@Test
void someTest() {
when(subject.someMethod(anyObject(), anyString(), anyListOf(String.class))).thenReturn(false);
}
}
After
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object objectMock;
private A subject;
@BeforeEach
void setup() {
subject = new A();
}
@Test
void someTest() {
when(subject.someMethod(any(), anyString(), anyList())).thenReturn(false);
}
}
@@ -3,0 +3,1 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -4,0 +5,1 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
@@ -5,3 +7,3 @@
import org.mockito.Mock;
-import static org.mockito.Matchers.anyListOf;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.anyString;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@@ -10,0 +12,1 @@
import static org.mockito.Mockito.when;
+@ExtendWith(MockitoExtension.class)
class MyTest {
@@ -23,1 +26,1 @@
@Test
void someTest() {
- when(subject.someMethod(anyObject(), anyString(), anyListOf(String.class))).thenReturn(false);
+ when(subject.someMethod(any(), anyString(), anyList())).thenReturn(false);
}
- xml
- Diff
Before
<project>
<groupId>org.example</groupId>
<artifactId>some-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.28.2</version>
</dependency>
</dependencies>
</project>
After
<project>
<groupId>org.example</groupId>
<artifactId>some-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.12.4</version>
</dependency>
</dependencies>
</project>
--- pom.xml
+++ pom.xml
@@ -13,2 +13,2 @@
<dependency>
<groupId>org.mockito</groupId>
- <artifactId>mockito-all</artifactId>
- <version>1.10.19</version>
+ <artifactId>mockito-core</artifactId>
+ <version>3.12.4</version>
</dependency>
@@ -19,1 +19,1 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
- <version>2.28.2</version>
+ <version>3.12.4</version>
</dependency>
java
- java
- Diff
Before
package mockito.example;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.mockito.ArgumentMatchers.anyListOf;
import static org.mockito.ArgumentMatchers.anySetOf;
import static org.mockito.ArgumentMatchers.anyMapOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockitoVarargMatcherTest {
public static class Foo {
public boolean addList(List<String> strings) { return true; }
public boolean addSet(Set<String> strings) { return true; }
public boolean addMap(Map<String, String> stringStringMap) { return true; }
}
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
when(mockFoo.addList(anyListOf(String.class))).thenReturn(true);
when(mockFoo.addSet(anySetOf(String.class))).thenReturn(true);
when(mockFoo.addMap(anyMapOf(String.class, String.class))).thenReturn(true);
}
}
After
package mockito.example;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockitoVarargMatcherTest {
public static class Foo {
public boolean addList(List<String> strings) { return true; }
public boolean addSet(Set<String> strings) { return true; }
public boolean addMap(Map<String, String> stringStringMap) { return true; }
}
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
when(mockFoo.addList(anyList())).thenReturn(true);
when(mockFoo.addSet(anySet())).thenReturn(true);
when(mockFoo.addMap(anyMap())).thenReturn(true);
}
}
@@ -7,3 +7,3 @@
import java.util.Set;
-import static org.mockito.ArgumentMatchers.anyListOf;
-import static org.mockito.ArgumentMatchers.anySetOf;
-import static org.mockito.ArgumentMatchers.anyMapOf;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anySet;
+import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
@@ -21,3 +21,3 @@
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
- when(mockFoo.addList(anyListOf(String.class))).thenReturn(true);
- when(mockFoo.addSet(anySetOf(String.class))).thenReturn(true);
- when(mockFoo.addMap(anyMapOf(String.class, String.class))).thenReturn(true);
+ when(mockFoo.addList(anyList())).thenReturn(true);
+ when(mockFoo.addSet(anySet())).thenReturn(true);
+ when(mockFoo.addMap(anyMap())).thenReturn(true);
}
groovy, java, xml
- groovy
- Diff
Before
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
testImplementation("org.mockito:mockito-all:1.10.19")
testImplementation("org.mockito:mockito-junit-jupiter:2.28.2")
}
test {
useJUnitPlatform()
}
After
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
testImplementation("org.mockito:mockito-core:3.12.4")
testImplementation("org.mockito:mockito-junit-jupiter:3.12.4")
}
test {
useJUnitPlatform()
}
--- build.gradle
+++ build.gradle
@@ -9,2 +9,2 @@
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
- testImplementation("org.mockito:mockito-all:1.10.19")
- testImplementation("org.mockito:mockito-junit-jupiter:2.28.2")
+ testImplementation("org.mockito:mockito-core:3.12.4")
+ testImplementation("org.mockito:mockito-junit-jupiter:3.12.4")
}
- java
- Diff
Before
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
class MyTest {
@Mock
Object objectMock;
private A subject;
@BeforeEach
void setup() {
subject = new A();
}
@Test
void someTest() {
when(subject.someMethod(anyObject(), anyString(), anyListOf(String.class))).thenReturn(false);
}
}
After
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
Object objectMock;
private A subject;
@BeforeEach
void setup() {
subject = new A();
}
@Test
void someTest() {
when(subject.someMethod(any(), anyString(), anyList())).thenReturn(false);
}
}
@@ -3,0 +3,1 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -4,0 +5,1 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
@@ -5,3 +7,3 @@
import org.mockito.Mock;
-import static org.mockito.Matchers.anyListOf;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.anyString;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@@ -10,0 +12,1 @@
import static org.mockito.Mockito.when;
+@ExtendWith(MockitoExtension.class)
class MyTest {
@@ -23,1 +26,1 @@
@Test
void someTest() {
- when(subject.someMethod(anyObject(), anyString(), anyListOf(String.class))).thenReturn(false);
+ when(subject.someMethod(any(), anyString(), anyList())).thenReturn(false);
}
- xml
- Diff
Before
<project>
<groupId>org.example</groupId>
<artifactId>some-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.28.2</version>
</dependency>
</dependencies>
</project>
After
<project>
<groupId>org.example</groupId>
<artifactId>some-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.12.4</version>
</dependency>
</dependencies>
</project>
--- pom.xml
+++ pom.xml
@@ -13,2 +13,2 @@
<dependency>
<groupId>org.mockito</groupId>
- <artifactId>mockito-all</artifactId>
- <version>1.10.19</version>
+ <artifactId>mockito-core</artifactId>
+ <version>3.12.4</version>
</dependency>
@@ -19,1 +19,1 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
- <version>2.28.2</version>
+ <version>3.12.4</version>
</dependency>
java
- java
- Diff
Before
package mockito.example;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.mockito.ArgumentMatchers.anyListOf;
import static org.mockito.ArgumentMatchers.anySetOf;
import static org.mockito.ArgumentMatchers.anyMapOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockitoVarargMatcherTest {
public static class Foo {
public boolean addList(List<String> strings) { return true; }
public boolean addSet(Set<String> strings) { return true; }
public boolean addMap(Map<String, String> stringStringMap) { return true; }
}
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
when(mockFoo.addList(anyListOf(String.class))).thenReturn(true);
when(mockFoo.addSet(anySetOf(String.class))).thenReturn(true);
when(mockFoo.addMap(anyMapOf(String.class, String.class))).thenReturn(true);
}
}
After
package mockito.example;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockitoVarargMatcherTest {
public static class Foo {
public boolean addList(List<String> strings) { return true; }
public boolean addSet(Set<String> strings) { return true; }
public boolean addMap(Map<String, String> stringStringMap) { return true; }
}
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
when(mockFoo.addList(anyList())).thenReturn(true);
when(mockFoo.addSet(anySet())).thenReturn(true);
when(mockFoo.addMap(anyMap())).thenReturn(true);
}
}
@@ -7,3 +7,3 @@
import java.util.Set;
-import static org.mockito.ArgumentMatchers.anyListOf;
-import static org.mockito.ArgumentMatchers.anySetOf;
-import static org.mockito.ArgumentMatchers.anyMapOf;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anySet;
+import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
@@ -21,3 +21,3 @@
public void usesVarargMatcher() {
Foo mockFoo = mock(Foo.class);
- when(mockFoo.addList(anyListOf(String.class))).thenReturn(true);
- when(mockFoo.addSet(anySetOf(String.class))).thenReturn(true);
- when(mockFoo.addMap(anyMapOf(String.class, String.class))).thenReturn(true);
+ when(mockFoo.addList(anyList())).thenReturn(true);
+ when(mockFoo.addSet(anySet())).thenReturn(true);
+ when(mockFoo.addMap(anyMap())).thenReturn(true);
}
Usage
Run this recipe
This recipe has no required configuration options. Users of Moderne can run it via the Moderne CLI.
You will need to have configured the Moderne CLI on your machine before you can run the following command.
shell
mod run . --recipe Mockito1to3Migration
If the recipe is not available locally, then you can install it using:
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:3.40.0
Data tables
Source files that had results
org.openrewrite.table.SourcesFileResultsSource files that were modified by the recipe run.
| Column | Description |
|---|---|
| Source path before the run | The source path of the file before the run. null when a source file was created during the run. |
| Source path after the run | A recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run. |
| Parent of the recipe that made changes | In a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all. |
| Recipe that made changes | The specific recipe that made a change. |
| Estimated time saving | An estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds. |
| Cycle | The recipe cycle in which the change was made. |
Source files that had search results
org.openrewrite.table.SearchResultsSearch results that were found during the recipe run.
| Column | Description |
|---|---|
| Source path of search result before the run | The source path of the file with the search result markers present. |
| Source path of search result after run the run | A recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run. |
| Result | The trimmed printed tree of the LST element that the marker is attached to. |
| Description | The content of the description of the marker. |
| Recipe that added the search marker | The specific recipe that added the Search marker. |
Source files that errored on a recipe
org.openrewrite.table.SourcesFileErrorsThe details of all errors produced by a recipe run.
| Column | Description |
|---|---|
| Source path | The file that failed to parse. |
| Recipe that made changes | The specific recipe that made a change. |
| Stack trace | The stack trace of the failure. |
Recipe performance
org.openrewrite.table.RecipeRunStatsStatistics used in analyzing the performance of recipes.
| Column | Description |
|---|---|
| The recipe | The recipe whose stats are being measured both individually and cumulatively. |
| Source file count | The number of source files the recipe ran over. |
| Source file changed count | The number of source files which were changed in the recipe run. Includes files created, deleted, and edited. |
| Cumulative scanning time (ns) | The total time spent across the scanning phase of this recipe. |
| Max scanning time (ns) | The max time scanning any one source file. |
| Cumulative edit time (ns) | The total time spent across the editing phase of this recipe. |
| Max edit time (ns) | The max time editing any one source file. |