From 2cc14091fbb7f61649489208384af4c5b18f8fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Rosen=C3=B6gger?= <123haynes@gmail.com> Date: Thu, 5 Dec 2024 22:06:30 +0100 Subject: [PATCH] Add ReplaceMockBeanAndSpyBean Recipe --- .../rewrite/replace-mock-and-spybean.yml | 28 ++ .../boot3/ReplaceMockBeanAndSpyBeanTest.java | 241 ++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 src/main/resources/META-INF/rewrite/replace-mock-and-spybean.yml create mode 100644 src/test/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBeanTest.java diff --git a/src/main/resources/META-INF/rewrite/replace-mock-and-spybean.yml b/src/main/resources/META-INF/rewrite/replace-mock-and-spybean.yml new file mode 100644 index 00000000..3508d81c --- /dev/null +++ b/src/main/resources/META-INF/rewrite/replace-mock-and-spybean.yml @@ -0,0 +1,28 @@ +# +# Copyright 2024 the original author or authors. +#

+# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#

+# https://www.apache.org/licenses/LICENSE-2.0 +#

+# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.boot3.ReplaceMockBeanAndSpyBean +displayName: Replace `@MockBean` and `@SpyBean` +description: Replaces `@MockBean` and `@SpyBean` annotations with `@MockitoBean` and `@MockitoSpyBean`. Also Update the relevant import statements. +recipeList: + - org.openrewrite.java.ChangeType: + oldFullyQualifiedTypeName: org.springframework.boot.test.mock.mockito.MockBean + newFullyQualifiedTypeName: org.springframework.test.context.bean.override.mockito.MockitoBean + - org.openrewrite.java.ChangeType: + oldFullyQualifiedTypeName: org.springframework.boot.test.mock.mockito.SpyBean + newFullyQualifiedTypeName: org.springframework.test.context.bean.override.mockito.MockitoSpyBean diff --git a/src/test/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBeanTest.java b/src/test/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBeanTest.java new file mode 100644 index 00000000..1f5ada30 --- /dev/null +++ b/src/test/java/org/openrewrite/java/spring/boot3/ReplaceMockBeanAndSpyBeanTest.java @@ -0,0 +1,241 @@ +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.recipeFromResources("org.openrewrite.java.boot3.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 replacesMockBeanWithParamsWithMockitoBeanWithParams() { + rewriteRun( + // Input source file before applying the recipe + java( + """ + import org.springframework.boot.test.mock.mockito.MockBean; + + public class SomeTest { + @MockBean(name="bean1") + private String someService; + } + """, + // Expected output after applying the recipe + """ + import org.springframework.test.context.bean.override.mockito.MockitoBean; + + public class SomeTest { + @MockitoBean(name="bean1") + 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; + } + """ + ) + ); + } +}