Skip to content

Commit c237a18

Browse files
authored
Upgrade to Mockito 5 and remove mockito-inline (#439)
* Change Mockito inline to core in Mockito 5.x Fixes #410 * Use orElseThrow
1 parent bc6a9e7 commit c237a18

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

src/main/resources/META-INF/rewrite/mockito.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
---
17+
type: specs.openrewrite.org/v1beta/recipe
18+
name: org.openrewrite.java.testing.mockito.Mockito1to5Migration
19+
displayName: Mockito 5.x upgrade
20+
description: Upgrade Mockito from 1.x to 5.x.
21+
tags:
22+
- testing
23+
- mockito
24+
recipeList:
25+
- org.openrewrite.java.testing.mockito.Mockito1to4Migration
26+
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
27+
groupId: org.mockito
28+
artifactId: "*"
29+
newVersion: 5.x
30+
- org.openrewrite.java.dependencies.ChangeDependency:
31+
oldGroupId: org.mockito
32+
oldArtifactId: mockito-inline
33+
newArtifactId: mockito-core
34+
- org.openrewrite.maven.RemoveDuplicateDependencies
35+
1636
---
1737
type: specs.openrewrite.org/v1beta/recipe
1838
name: org.openrewrite.java.testing.mockito.Mockito1to4Migration
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 org.junit.jupiter.api.Test;
19+
import org.openrewrite.test.RecipeSpec;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import java.util.regex.Pattern;
23+
24+
import static org.openrewrite.maven.Assertions.pomXml;
25+
26+
class MockitoInlineToCoreTest implements RewriteTest {
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResources("org.openrewrite.java.testing.mockito.Mockito1to5Migration");
30+
}
31+
32+
@Test
33+
void inlineToCore() {
34+
rewriteRun(
35+
//language=xml
36+
pomXml(
37+
"""
38+
<project>
39+
<modelVersion>4.0.0</modelVersion>
40+
<groupId>com.example</groupId>
41+
<artifactId>demo</artifactId>
42+
<version>0.0.1-SNAPSHOT</version>
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-inline</artifactId>
47+
<version>3.11.2</version>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
</project>
52+
""",
53+
sourceSpecs -> sourceSpecs.after(after -> {
54+
String version = Pattern.compile("<version>(5.+)</version>").matcher(after).results().reduce((a, b) -> b).orElseThrow().group(1);
55+
return """
56+
<project>
57+
<modelVersion>4.0.0</modelVersion>
58+
<groupId>com.example</groupId>
59+
<artifactId>demo</artifactId>
60+
<version>0.0.1-SNAPSHOT</version>
61+
<dependencies>
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
65+
<version>%s</version>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
</project>
70+
""".formatted(version);
71+
})
72+
)
73+
);
74+
}
75+
76+
@Test
77+
void noDuplicates() {
78+
rewriteRun(
79+
//language=xml
80+
pomXml(
81+
"""
82+
<project>
83+
<modelVersion>4.0.0</modelVersion>
84+
<groupId>com.example</groupId>
85+
<artifactId>demo</artifactId>
86+
<version>0.0.1-SNAPSHOT</version>
87+
<dependencies>
88+
<dependency>
89+
<groupId>org.mockito</groupId>
90+
<artifactId>mockito-core</artifactId>
91+
<version>3.11.2</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.mockito</groupId>
96+
<artifactId>mockito-inline</artifactId>
97+
<version>3.11.2</version>
98+
<scope>test</scope>
99+
</dependency>
100+
</dependencies>
101+
</project>
102+
""",
103+
sourceSpecs -> sourceSpecs.after(after -> {
104+
String version = Pattern.compile("<version>(5.+)</version>").matcher(after).results().reduce((a, b) -> b).orElseThrow().group(1);
105+
return """
106+
<project>
107+
<modelVersion>4.0.0</modelVersion>
108+
<groupId>com.example</groupId>
109+
<artifactId>demo</artifactId>
110+
<version>0.0.1-SNAPSHOT</version>
111+
<dependencies>
112+
<dependency>
113+
<groupId>org.mockito</groupId>
114+
<artifactId>mockito-core</artifactId>
115+
<version>%s</version>
116+
<scope>test</scope>
117+
</dependency>
118+
</dependencies>
119+
</project>
120+
""".formatted(version);
121+
})
122+
)
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)