-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow @MockInBean to be used in meta-annotations (#25)
Co-authored-by: Ivan_Kyryliuk <[email protected]>
- Loading branch information
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
src/test/java/com/teketik/test/mockinbean/test/MockInBeanMetaAnnotationsTest.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,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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/com/teketik/test/mockinbean/test/annotations/MockInBeanWithCustomName.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,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 { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/com/teketik/test/mockinbean/test/annotations/MockInMultipleComponents.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,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 { | ||
} |