Skip to content

Commit 8047a22

Browse files
committed
Polish TestBeanOverrideProcessor
1 parent f7e7d1b commit 8047a22

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java

+29-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
24+
import java.util.ArrayList;
2425
import java.util.Arrays;
2526
import java.util.LinkedHashSet;
2627
import java.util.List;
@@ -40,7 +41,7 @@
4041

4142
/**
4243
* {@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
4445
* ensures that a corresponding static factory method exists, according to the
4546
* {@linkplain TestBean documented conventions}.
4647
*
@@ -50,6 +51,15 @@
5051
*/
5152
class TestBeanOverrideProcessor implements BeanOverrideProcessor {
5253

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+
5363
/**
5464
* Find a test bean factory {@link Method} in the given {@link Class} or one
5565
* of its parent classes, which meets the following criteria.
@@ -61,7 +71,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
6171
* </ul>
6272
* <p>If the test class inherits from another class, the class hierarchy is
6373
* 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,
6575
* provided they have the same name. However, if multiple methods are found
6676
* that match distinct candidate names, an exception is thrown.
6777
* @param clazz the class in which to search for the factory method
@@ -71,9 +81,9 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
7181
* @throws IllegalStateException if a matching factory method cannot
7282
* be found or multiple methods match
7383
*/
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);
7787
List<Method> methods = Arrays.stream(ReflectionUtils.getAllDeclaredMethods(clazz))
7888
.filter(method -> Modifier.isStatic(method.getModifiers()) &&
7989
supportedNames.contains(method.getName()) &&
@@ -110,27 +120,26 @@ public TestBeanOverrideMetadata createMetadata(Annotation overrideAnnotation, Cl
110120
TestBeanOverrideProcessor.class.getSimpleName(), field.getDeclaringClass().getName(),
111121
field.getName()));
112122
}
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);
119128
}
120129
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+
121135
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);
130138
}
139+
overrideMethod = findTestBeanFactoryMethod(testClass, field.getType(), candidateMethodNames);
131140
}
132141

133-
return new TestBeanOverrideMetadata(field, explicitOverrideMethod, testBeanAnnotation, ResolvableType.forField(field, testClass));
142+
return new TestBeanOverrideMetadata(field, overrideMethod, testBeanAnnotation, ResolvableType.forField(field, testClass));
134143
}
135144

136145

0 commit comments

Comments
 (0)