Skip to content

Commit 168f433

Browse files
authored
Fix AnyToNullable removing any() import when untyped any() is still used (#354) (#960)
Also migrate no-arg any() from Mockito to ArgumentMatchers to prevent the import from being dropped when typed any(Class) is converted to nullable(Class).
1 parent 144f337 commit 168f433

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/main/java/org/openrewrite/java/testing/mockito/AnyToNullable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean acc) {
6161
doAfterVisit(new ChangeMethodName(
6262
"org.mockito.Mockito any(java.lang.Class)", "nullable", null, null).getVisitor());
6363
doAfterVisit(new ChangeMethodTargetToStatic("org.mockito.Mockito nullable(java.lang.Class)", "org.mockito.ArgumentMatchers", null, null, false).getVisitor());
64+
doAfterVisit(new ChangeMethodTargetToStatic("org.mockito.Mockito any()", "org.mockito.ArgumentMatchers", null, null, false).getVisitor());
6465
doAfterVisit(new AnyStringToNullable().getVisitor());
6566
}
6667
return super.preVisit(tree, ctx);

src/test/java/org/openrewrite/java/testing/mockito/AnyToNullableTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,69 @@ void test() {
9999
);
100100
}
101101

102+
@Test
103+
void shouldNotRemoveAnyImportWhenUntypedAnyIsStillUsed() {
104+
//language=java
105+
rewriteRun(
106+
//language=xml
107+
pomXml(
108+
"""
109+
<project>
110+
<modelVersion>4.0.0</modelVersion>
111+
<groupId>com.example</groupId>
112+
<artifactId>foo</artifactId>
113+
<version>1.0.0</version>
114+
<dependencies>
115+
<dependency>
116+
<groupId>org.mockito</groupId>
117+
<artifactId>mockito-all</artifactId>
118+
<version>1.10.19</version>
119+
</dependency>
120+
</dependencies>
121+
</project>
122+
"""
123+
),
124+
//language=java
125+
java(
126+
"""
127+
class Example {
128+
String greet(Object obj, Object obj2) {
129+
return "Hello " + obj + obj2;
130+
}
131+
}
132+
"""
133+
),
134+
//language=java
135+
java(
136+
"""
137+
import static org.mockito.Mockito.mock;
138+
import static org.mockito.Mockito.when;
139+
import static org.mockito.Mockito.any;
140+
141+
class MyTest {
142+
void test() {
143+
Example example = mock(Example.class);
144+
when(example.greet(any(Object.class), any())).thenReturn("Hello world");
145+
}
146+
}
147+
""",
148+
"""
149+
import static org.mockito.ArgumentMatchers.any;
150+
import static org.mockito.ArgumentMatchers.nullable;
151+
import static org.mockito.Mockito.mock;
152+
import static org.mockito.Mockito.when;
153+
154+
class MyTest {
155+
void test() {
156+
Example example = mock(Example.class);
157+
when(example.greet(nullable(Object.class), any())).thenReturn("Hello world");
158+
}
159+
}
160+
"""
161+
)
162+
);
163+
}
164+
102165
@Test
103166
void doesNotTouchIfMockitoTwoPlus() {
104167
//language=java

0 commit comments

Comments
 (0)