|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Field;
|
20 | 20 | import java.lang.reflect.Method;
|
| 21 | +import java.util.List; |
21 | 22 | import java.util.Objects;
|
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test;
|
|
31 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
32 | 33 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
33 | 34 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 35 | +import static org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor.findTestBeanFactoryMethod; |
34 | 36 |
|
35 | 37 | /**
|
36 | 38 | * Tests for {@link TestBeanOverrideProcessor}.
|
37 | 39 | *
|
38 | 40 | * @author Simon Baslé
|
| 41 | + * @author Sam Brannen |
39 | 42 | * @since 6.2
|
40 | 43 | */
|
41 | 44 | class TestBeanOverrideProcessorTests {
|
42 | 45 |
|
43 | 46 | @Test
|
44 |
| - void ensureMethodFindsFromList() { |
45 |
| - Method method = TestBeanOverrideProcessor.ensureMethod(MethodConventionConf.class, ExampleService.class, |
46 |
| - "example1", "example2", "example3"); |
| 47 | + void findTestBeanFactoryMethodFindsFromCandidateNames() { |
| 48 | + Class<?> clazz = MethodConventionConf.class; |
| 49 | + Class<?> returnType = ExampleService.class; |
| 50 | + |
| 51 | + Method method = findTestBeanFactoryMethod(clazz, returnType, "example1", "example2", "example3"); |
47 | 52 |
|
48 | 53 | assertThat(method.getName()).isEqualTo("example2");
|
49 | 54 | }
|
50 | 55 |
|
51 | 56 | @Test
|
52 |
| - void ensureMethodNotFound() { |
| 57 | + void findTestBeanFactoryMethodNotFound() { |
| 58 | + Class<?> clazz = MethodConventionConf.class; |
| 59 | + Class<?> returnType = ExampleService.class; |
| 60 | + |
53 | 61 | assertThatIllegalStateException()
|
54 |
| - .isThrownBy(() -> TestBeanOverrideProcessor.ensureMethod(MethodConventionConf.class, ExampleService.class, |
55 |
| - "example1", "example3")) |
56 |
| - .withMessage("Found 0 static methods instead of exactly one, matching a name in [example1, example3] with return type " + |
57 |
| - ExampleService.class.getName() + " on class " + MethodConventionConf.class.getName()); |
| 62 | + .isThrownBy(() -> findTestBeanFactoryMethod(clazz, returnType, "example1", "example3")) |
| 63 | + .withMessage(""" |
| 64 | + Failed to find a static test bean factory method in %s with return type %s \ |
| 65 | + whose name matches one of the supported candidates %s""", |
| 66 | + clazz.getName(), returnType.getName(), List.of("example1", "example3")); |
58 | 67 | }
|
59 | 68 |
|
60 | 69 | @Test
|
61 |
| - void ensureMethodTwoFound() { |
| 70 | + void findTestBeanFactoryMethodTwoFound() { |
| 71 | + Class<?> clazz = MethodConventionConf.class; |
| 72 | + Class<?> returnType = ExampleService.class; |
| 73 | + |
62 | 74 | assertThatIllegalStateException()
|
63 |
| - .isThrownBy(() -> TestBeanOverrideProcessor.ensureMethod(MethodConventionConf.class, ExampleService.class, |
64 |
| - "example2", "example4")) |
65 |
| - .withMessage("Found 2 static methods instead of exactly one, matching a name in [example2, example4] with return type " + |
66 |
| - ExampleService.class.getName() + " on class " + MethodConventionConf.class.getName()); |
| 75 | + .isThrownBy(() -> findTestBeanFactoryMethod(clazz, returnType, "example2", "example4")) |
| 76 | + .withMessage(""" |
| 77 | + Found %d competing static test bean factory methods in %s with return type %s \ |
| 78 | + whose name matches one of the supported candidates %s""".formatted( |
| 79 | + 2, clazz.getName(), returnType.getName(), List.of("example2", "example4"))); |
67 | 80 | }
|
68 | 81 |
|
69 | 82 | @Test
|
70 |
| - void ensureMethodNoNameProvided() { |
| 83 | + void findTestBeanFactoryMethodNoNameProvided() { |
71 | 84 | assertThatIllegalArgumentException()
|
72 |
| - .isThrownBy(() -> TestBeanOverrideProcessor.ensureMethod(MethodConventionConf.class, ExampleService.class)) |
73 |
| - .withMessage("At least one expectedMethodName is required"); |
| 85 | + .isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionConf.class, ExampleService.class)) |
| 86 | + .withMessage("At least one candidate method name is required"); |
74 | 87 | }
|
75 | 88 |
|
76 | 89 | @Test
|
77 |
| - void createMetaDataForUnknownExplicitMethod() throws NoSuchFieldException { |
78 |
| - Field field = ExplicitMethodNameConf.class.getField("a"); |
| 90 | + void createMetaDataForUnknownExplicitMethod() throws Exception { |
| 91 | + Class<?> clazz = ExplicitMethodNameConf.class; |
| 92 | + Class<?> returnType = ExampleService.class; |
| 93 | + Field field = clazz.getField("a"); |
79 | 94 | TestBean overrideAnnotation = Objects.requireNonNull(field.getAnnotation(TestBean.class));
|
| 95 | + |
80 | 96 | TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
|
81 | 97 | assertThatIllegalStateException()
|
82 |
| - .isThrownBy(() -> processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(ExampleService.class))) |
83 |
| - .withMessage("Found 0 static methods instead of exactly one, matching a name in [explicit1] with return type " + |
84 |
| - ExampleService.class.getName() + " on class " + ExplicitMethodNameConf.class.getName()); |
| 98 | + .isThrownBy(() -> processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(returnType))) |
| 99 | + .withMessage(""" |
| 100 | + Failed to find a static test bean factory method in %s with return type %s \ |
| 101 | + whose name matches one of the supported candidates %s""", |
| 102 | + clazz.getName(), returnType.getName(), List.of("explicit1")); |
85 | 103 | }
|
86 | 104 |
|
87 | 105 | @Test
|
88 |
| - void createMetaDataForKnownExplicitMethod() throws NoSuchFieldException { |
| 106 | + void createMetaDataForKnownExplicitMethod() throws Exception { |
| 107 | + Class<?> returnType = ExampleService.class; |
89 | 108 | Field field = ExplicitMethodNameConf.class.getField("b");
|
90 | 109 | TestBean overrideAnnotation = Objects.requireNonNull(field.getAnnotation(TestBean.class));
|
| 110 | + |
91 | 111 | TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
|
92 |
| - assertThat(processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(ExampleService.class))) |
| 112 | + assertThat(processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(returnType))) |
93 | 113 | .isInstanceOf(MethodConventionOverrideMetadata.class);
|
94 | 114 | }
|
95 | 115 |
|
96 | 116 | @Test
|
97 |
| - void createMetaDataWithDeferredEnsureMethodCheck() throws NoSuchFieldException { |
| 117 | + void createMetaDataWithDeferredCheckForExistenceOfConventionBasedFactoryMethod() throws Exception { |
| 118 | + Class<?> returnType = ExampleService.class; |
98 | 119 | Field field = MethodConventionConf.class.getField("field");
|
99 | 120 | TestBean overrideAnnotation = Objects.requireNonNull(field.getAnnotation(TestBean.class));
|
| 121 | + |
100 | 122 | TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
|
101 |
| - assertThat(processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(ExampleService.class))) |
| 123 | + // When in convention-based mode, createMetadata() will not verify that |
| 124 | + // the factory method actually exists. So, we don't expect an exception |
| 125 | + // for this use case. |
| 126 | + assertThat(processor.createMetadata(field, overrideAnnotation, ResolvableType.forClass(returnType))) |
102 | 127 | .isInstanceOf(MethodConventionOverrideMetadata.class);
|
103 | 128 | }
|
104 | 129 |
|
|
0 commit comments