From 999d54c7c080c51726ff496e0041511c6943b4e7 Mon Sep 17 00:00:00 2001 From: kyriosX Date: Sat, 10 Aug 2024 15:46:42 +0200 Subject: [PATCH] Allow @MockInBean to be used in meta-annotations (#25) Co-authored-by: Ivan_Kyryliuk --- .../teketik/test/mockinbean/MockInBean.java | 2 +- .../teketik/test/mockinbean/MockInBeans.java | 2 +- .../test/MockInBeanMetaAnnotationsTest.java | 43 +++++++++++++++++++ .../annotations/MockInBeanWithCustomName.java | 15 +++++++ .../annotations/MockInMultipleComponents.java | 17 ++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/teketik/test/mockinbean/test/MockInBeanMetaAnnotationsTest.java create mode 100644 src/test/java/com/teketik/test/mockinbean/test/annotations/MockInBeanWithCustomName.java create mode 100644 src/test/java/com/teketik/test/mockinbean/test/annotations/MockInMultipleComponents.java diff --git a/src/main/java/com/teketik/test/mockinbean/MockInBean.java b/src/main/java/com/teketik/test/mockinbean/MockInBean.java index d3422c3..e927eae 100644 --- a/src/main/java/com/teketik/test/mockinbean/MockInBean.java +++ b/src/main/java/com/teketik/test/mockinbean/MockInBean.java @@ -66,7 +66,7 @@ * @author Antoine Meyer * @see SpyInBean */ -@Target({ElementType.FIELD}) +@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(MockInBeans.class) public @interface MockInBean { diff --git a/src/main/java/com/teketik/test/mockinbean/MockInBeans.java b/src/main/java/com/teketik/test/mockinbean/MockInBeans.java index f56ccf6..97b78e5 100644 --- a/src/main/java/com/teketik/test/mockinbean/MockInBeans.java +++ b/src/main/java/com/teketik/test/mockinbean/MockInBeans.java @@ -16,7 +16,7 @@ * * @author Antoine Meyer */ -@Target({ElementType.FIELD}) +@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MockInBeans { diff --git a/src/test/java/com/teketik/test/mockinbean/test/MockInBeanMetaAnnotationsTest.java b/src/test/java/com/teketik/test/mockinbean/test/MockInBeanMetaAnnotationsTest.java new file mode 100644 index 0000000..ebf9521 --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/MockInBeanMetaAnnotationsTest.java @@ -0,0 +1,43 @@ +package com.teketik.test.mockinbean.test; + +import com.teketik.test.mockinbean.test.annotations.MockInBeanWithCustomName; +import com.teketik.test.mockinbean.test.annotations.MockInMultipleComponents; +import com.teketik.test.mockinbean.test.components.MockableComponent1; +import com.teketik.test.mockinbean.test.components.MockableComponent2; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.test.util.ReflectionTestUtils; + +class MockInBeanMetaAnnotationsTest extends MockInBeanBaseTest { + + @MockInMultipleComponents + private MockableComponent1 mockableComponent1; + + @MockInBeanWithCustomName + private MockableComponent2 mockableComponent2; + + /** + * Note: Will fail if run individually, run as a class. + */ + @Test + void test() { + Assertions.assertTrue(TestUtils.isMock(mockableComponent1)); + Assertions.assertSame(mockableComponent1, ReflectionTestUtils.getField(testComponent1, "mockableComponent1")); + Assertions.assertSame(mockableComponent1, ReflectionTestUtils.getField(testComponent2, "mockableComponent1")); + + Assertions.assertTrue(TestUtils.isMock(mockableComponent2)); + Assertions.assertSame(mockableComponent2, ReflectionTestUtils.getField(testComponent1, "mockableComponent2")); + Assertions.assertNotSame(mockableComponent2, ReflectionTestUtils.getField(testComponent2, "mockableComponent2")); + Assertions.assertFalse(TestUtils.isMockOrSpy(ReflectionTestUtils.getField(testComponent2, "mockableComponent2"))); + } + + @Override + MockableComponent1 getMockableComponent1() { + return mockableComponent1; + } + + @Override + MockableComponent2 getMockableComponent2() { + return mockableComponent2; + } +} diff --git a/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInBeanWithCustomName.java b/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInBeanWithCustomName.java new file mode 100644 index 0000000..bde9771 --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInBeanWithCustomName.java @@ -0,0 +1,15 @@ +package com.teketik.test.mockinbean.test.annotations; + +import com.teketik.test.mockinbean.MockInBean; +import com.teketik.test.mockinbean.test.components.TestComponent1; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@MockInBean(TestComponent1.class) +public @interface MockInBeanWithCustomName { +} diff --git a/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInMultipleComponents.java b/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInMultipleComponents.java new file mode 100644 index 0000000..866fe89 --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/annotations/MockInMultipleComponents.java @@ -0,0 +1,17 @@ +package com.teketik.test.mockinbean.test.annotations; + +import com.teketik.test.mockinbean.MockInBean; +import com.teketik.test.mockinbean.test.components.TestComponent1; +import com.teketik.test.mockinbean.test.components.TestComponent2; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@MockInBean(TestComponent1.class) +@MockInBean(TestComponent2.class) +public @interface MockInMultipleComponents { +}