-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReplaceMockBeanAndSpyBean Recipe
- Loading branch information
Showing
2 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.openrewrite.java.spring.boot3; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.java.AnnotationMatcher; | ||
import org.openrewrite.java.ChangeType; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.J; | ||
|
||
public class ReplaceMockBeanAndSpyBean extends Recipe { | ||
private static final AnnotationMatcher MOCKBEAN_ANNOTATION_MATCHER = | ||
new AnnotationMatcher("@org.springframework.boot.test.mock.mockito.MockBean"); | ||
private static final AnnotationMatcher SPYBEAN_ANNOTATION_MATCHER = | ||
new AnnotationMatcher("@org.springframework.boot.test.mock.mockito.SpyBean"); | ||
|
||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace @MockBean and @SpyBean"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replaces `@MockBean` and `@SpyBean` annotations with `@MockitoBean` and `@MockitoSpyBean`. " + | ||
"Also Update the relevant import statements."; | ||
} | ||
|
||
@Override | ||
public JavaIsoVisitor<ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) { | ||
|
||
J.Annotation a = super.visitAnnotation(annotation, ctx); | ||
|
||
// Check if the annotation is @MockBean | ||
if (MOCKBEAN_ANNOTATION_MATCHER.matches(a)) { | ||
//remove the old import and add the new one | ||
maybeRemoveImport("org.springframework.boot.test.mock.mockito.MockBean"); | ||
maybeAddImport("org.springframework.test.context.bean.override.mockito.MockitoBean"); | ||
|
||
//Change the annotation | ||
a = (J.Annotation) new ChangeType("org.springframework.boot.test.mock.mockito.MockBean", | ||
"org.springframework.test.context.bean.override.mockito.MockitoBean", false) | ||
.getVisitor().visit(a, ctx, getCursor().getParentOrThrow()); | ||
} | ||
|
||
// Check if the annotation is @SpyBean | ||
if (SPYBEAN_ANNOTATION_MATCHER.matches(a)) { | ||
//remove the old import and add the new one | ||
maybeRemoveImport("org.springframework.boot.test.mock.mockito.SpyBean"); | ||
maybeAddImport("org.springframework.test.context.bean.override.mockito.MockitoSpyBean"); | ||
|
||
//Change the annotation | ||
a = (J.Annotation) new ChangeType("org.springframework.boot.test.mock.mockito.SpyBean", | ||
"org.springframework.test.context.bean.override.mockito.MockitoSpyBean", false) | ||
.getVisitor().visit(a, ctx, getCursor().getParentOrThrow()); | ||
} | ||
return a != null ? a : annotation; | ||
} | ||
}; | ||
} | ||
} |
215 changes: 215 additions & 0 deletions
215
src/test/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package org.openrewrite.java.spring.boot3; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class ReplaceMockBeanAndSpyBeanTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ReplaceMockBeanAndSpyBean()) | ||
.parser(JavaParser.fromJavaVersion() | ||
.classpathFromResources(new InMemoryExecutionContext(), | ||
"spring-boot-test")); | ||
} | ||
|
||
@Test | ||
void replacesMockBeanWithMockitoBean() { | ||
rewriteRun( | ||
// Input source file before applying the recipe | ||
java( | ||
""" | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
public class SomeTest { | ||
@MockBean | ||
private String someService; | ||
} | ||
""", | ||
// Expected output after applying the recipe | ||
""" | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
public class SomeTest { | ||
@MockitoBean | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNotChangeOtherAnnotations() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
public class SomeTest { | ||
@MockBean | ||
@Deprecated | ||
private String someService; | ||
} | ||
""", | ||
""" | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
public class SomeTest { | ||
@MockitoBean | ||
@Deprecated | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void handlesNoMockBeanImport() { | ||
rewriteRun( | ||
java( | ||
""" | ||
public class SomeTest { | ||
@org.springframework.boot.test.mock.mockito.MockBean | ||
private String someService; | ||
} | ||
""", | ||
""" | ||
public class SomeTest { | ||
@org.springframework.test.context.bean.override.mockito.MockitoBean | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replacesMockBeanWithMockitoBeanAndSpyBeanWithMockitoSpyBean() { | ||
rewriteRun( | ||
// Input source file before applying the recipe | ||
java( | ||
""" | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
public class SomeTest { | ||
@SpyBean | ||
private String someService; | ||
@MockBean | ||
private String someMockService; | ||
} | ||
""", | ||
// Expected output after applying the recipe | ||
""" | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; | ||
public class SomeTest { | ||
@MockitoSpyBean | ||
private String someService; | ||
@MockitoBean | ||
private String someMockService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replacesSpyBeanWithMockitoSpyBean() { | ||
rewriteRun( | ||
// Input source file before applying the recipe | ||
java( | ||
""" | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
public class SomeTest { | ||
@SpyBean | ||
private String someService; | ||
} | ||
""", | ||
// Expected output after applying the recipe | ||
""" | ||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; | ||
public class SomeTest { | ||
@MockitoSpyBean | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNotChangeOtherAnnotationsSpyBean() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
public class SomeTest { | ||
@SpyBean | ||
@Deprecated | ||
private String someService; | ||
} | ||
""", | ||
""" | ||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; | ||
public class SomeTest { | ||
@MockitoSpyBean | ||
@Deprecated | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void handlesNoSpyBeanImport() { | ||
rewriteRun( | ||
java( | ||
""" | ||
public class SomeTest { | ||
@org.springframework.boot.test.mock.mockito.SpyBean | ||
private String someService; | ||
} | ||
""", | ||
""" | ||
public class SomeTest { | ||
@org.springframework.test.context.bean.override.mockito.MockitoSpyBean | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNothingWhenNoAnnotationPresent() { | ||
rewriteRun( | ||
java( | ||
""" | ||
public class SomeTest { | ||
private String someService; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |