Skip to content

Commit

Permalink
Add a NoMissingTypes Precondition to CleanupMockitoImports to prevent…
Browse files Browse the repository at this point in the history
… changes if types are missing
  • Loading branch information
gideon-sunbit committed Jun 25, 2024
1 parent 9eabd19 commit b2d222a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
Expand All @@ -47,7 +48,12 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.mockito.*", false), new CleanupMockitoImportsVisitor());
return Preconditions.check(
Preconditions.and(
new NoMissingTypes(),
new UsesType<>("org.mockito.*", false)
),
new CleanupMockitoImportsVisitor());
}

private static class CleanupMockitoImportsVisitor extends JavaIsoVisitor<ExecutionContext> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -281,4 +282,30 @@ public class MockitoArgumentMatchersTest {
)
);
}

@Test
void doNotRemoveImportsIfMissingTypeInformation() {
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
java(
"""
import org.mockito.kotlin.times;
import static org.mockito.Mockito.when;
class MyObjectTest {
MyObject myObject;
void test() {
when(myObject.getSomeField()).thenReturn("testValue");
verify(myObject, times(1)).getSomeField();
}
}
class MyObject {
String getSomeField() { return null; }
}
"""
)
);
}
}

0 comments on commit b2d222a

Please sign in to comment.