From 999d54c7c080c51726ff496e0041511c6943b4e7 Mon Sep 17 00:00:00 2001 From: kyriosX Date: Sat, 10 Aug 2024 15:46:42 +0200 Subject: [PATCH 1/2] 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 { +} From e417bb0087c59ff32d703684db1fef396394a0bd Mon Sep 17 00:00:00 2001 From: antoine Date: Sat, 10 Aug 2024 08:59:27 -0500 Subject: [PATCH 2/2] update documentation for v1.7 --- README.MD | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.MD b/README.MD index 05e15d7..37f76fd 100644 --- a/README.MD +++ b/README.MD @@ -86,7 +86,7 @@ Simply include the maven dependency (from central maven) to start using @MockInB com.teketik mock-in-bean - boot2-v1.6 + boot2-v1.7 test ``` diff --git a/pom.xml b/pom.xml index c9091e6..d55844c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.teketik mock-in-bean - boot2-v1.6 + boot2-v1.7 Mock in Bean Surgically Inject Mockito Mock/Spy in Spring Beans https://github.com/antoinemeyer/mock-in-bean