21
21
import java .lang .reflect .InvocationTargetException ;
22
22
import java .lang .reflect .Method ;
23
23
import java .lang .reflect .Modifier ;
24
+ import java .util .ArrayList ;
24
25
import java .util .Arrays ;
25
26
import java .util .LinkedHashSet ;
26
27
import java .util .List ;
40
41
41
42
/**
42
43
* {@link BeanOverrideProcessor} implementation for {@link TestBean @TestBean}
43
- * support. It creates metadata for annotated fields in a given class and
44
+ * support, which creates metadata for annotated fields in a given class and
44
45
* ensures that a corresponding static factory method exists, according to the
45
46
* {@linkplain TestBean documented conventions}.
46
47
*
50
51
*/
51
52
class TestBeanOverrideProcessor implements BeanOverrideProcessor {
52
53
54
+ /**
55
+ * Find a test bean factory {@link Method} in the given {@link Class} or one
56
+ * of its parent classes.
57
+ * <p>Delegates to {@link #findTestBeanFactoryMethod(Class, Class, List)}.
58
+ */
59
+ static Method findTestBeanFactoryMethod (Class <?> clazz , Class <?> methodReturnType , String ... methodNames ) {
60
+ return findTestBeanFactoryMethod (clazz , methodReturnType , List .of (methodNames ));
61
+ }
62
+
53
63
/**
54
64
* Find a test bean factory {@link Method} in the given {@link Class} or one
55
65
* of its parent classes, which meets the following criteria.
@@ -61,7 +71,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
61
71
* </ul>
62
72
* <p>If the test class inherits from another class, the class hierarchy is
63
73
* searched for factory methods. Matching factory methods are prioritized
64
- * from closest to furthest from the test class in the class hierarchy,
74
+ * from closest to farthest from the test class in the class hierarchy,
65
75
* provided they have the same name. However, if multiple methods are found
66
76
* that match distinct candidate names, an exception is thrown.
67
77
* @param clazz the class in which to search for the factory method
@@ -71,9 +81,9 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
71
81
* @throws IllegalStateException if a matching factory method cannot
72
82
* be found or multiple methods match
73
83
*/
74
- static Method findTestBeanFactoryMethod (Class <?> clazz , Class <?> methodReturnType , String ... methodNames ) {
75
- Assert .isTrue (methodNames . length > 0 , "At least one candidate method name is required" );
76
- Set <String > supportedNames = new LinkedHashSet <>(Arrays . asList ( methodNames ) );
84
+ static Method findTestBeanFactoryMethod (Class <?> clazz , Class <?> methodReturnType , List < String > methodNames ) {
85
+ Assert .notEmpty (methodNames , "At least one candidate method name is required" );
86
+ Set <String > supportedNames = new LinkedHashSet <>(methodNames );
77
87
List <Method > methods = Arrays .stream (ReflectionUtils .getAllDeclaredMethods (clazz ))
78
88
.filter (method -> Modifier .isStatic (method .getModifiers ()) &&
79
89
supportedNames .contains (method .getName ()) &&
@@ -110,27 +120,26 @@ public TestBeanOverrideMetadata createMetadata(Annotation overrideAnnotation, Cl
110
120
TestBeanOverrideProcessor .class .getSimpleName (), field .getDeclaringClass ().getName (),
111
121
field .getName ()));
112
122
}
113
- // If the user specified a method explicitly, search for that.
114
- // Otherwise, search candidate factory methods using the convention suffix
115
- // and the explicit bean name (if any) or field name.
116
- Method explicitOverrideMethod ;
117
- if (!testBeanAnnotation .methodName ().isBlank ()) {
118
- explicitOverrideMethod = findTestBeanFactoryMethod (testClass , field .getType (), testBeanAnnotation .methodName ());
123
+ Method overrideMethod ;
124
+ String methodName = testBeanAnnotation .methodName ();
125
+ if (!methodName .isBlank ()) {
126
+ // If the user specified an explicit method name, search for that.
127
+ overrideMethod = findTestBeanFactoryMethod (testClass , field .getType (), methodName );
119
128
}
120
129
else {
130
+ // Otherwise, search for candidate factory methods using the convention
131
+ // suffix and the field name or explicit bean name (if any).
132
+ List <String > candidateMethodNames = new ArrayList <>();
133
+ candidateMethodNames .add (field .getName () + TestBean .CONVENTION_SUFFIX );
134
+
121
135
String beanName = testBeanAnnotation .name ();
122
- if (!StringUtils .hasText (beanName )) {
123
- explicitOverrideMethod = findTestBeanFactoryMethod (testClass , field .getType (),
124
- field .getName () + TestBean .CONVENTION_SUFFIX );
125
- }
126
- else {
127
- explicitOverrideMethod = findTestBeanFactoryMethod (testClass , field .getType (),
128
- beanName + TestBean .CONVENTION_SUFFIX ,
129
- field .getName () + TestBean .CONVENTION_SUFFIX );
136
+ if (StringUtils .hasText (beanName )) {
137
+ candidateMethodNames .add (beanName + TestBean .CONVENTION_SUFFIX );
130
138
}
139
+ overrideMethod = findTestBeanFactoryMethod (testClass , field .getType (), candidateMethodNames );
131
140
}
132
141
133
- return new TestBeanOverrideMetadata (field , explicitOverrideMethod , testBeanAnnotation , ResolvableType .forField (field , testClass ));
142
+ return new TestBeanOverrideMetadata (field , overrideMethod , testBeanAnnotation , ResolvableType .forField (field , testClass ));
134
143
}
135
144
136
145
0 commit comments