Skip to content

Commit

Permalink
Add ReplaceMockBeanAndSpyBean Recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
123Haynes committed Dec 5, 2024
1 parent 6738e13 commit a3b1eb0
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 0 deletions.
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;
}
};
}
}
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;
}
"""
)
);
}
}

0 comments on commit a3b1eb0

Please sign in to comment.