Support fully-qualified factory method names in @⁠TestBean

Prior to this commit, @⁠TestBean factory methods had to be defined in
the test class, one of its superclasses, or in an implemented
interface. However, users may wish to define common factory methods in
external classes that can be shared easily across multiple test classes
simply by referencing an external method via a fully-qualified method
name.

To address that, this commit introduces support for referencing a
@⁠TestBean factory method via its fully-qualified method name following
the syntax <fully-qualified class name>#<method name>.

Closes gh-33125
This commit is contained in:
Sam Brannen
2024-06-30 16:19:13 +02:00
parent c2f8d4803f
commit cc002875c4
5 changed files with 170 additions and 11 deletions

View File

@@ -40,8 +40,12 @@ import org.springframework.test.context.bean.override.BeanOverride;
* test class whose return type is compatible with the annotated field. In the
* case of a nested test class, the enclosing class hierarchy is also searched.
* Similarly, if the test class extends from a base class or implements any
* interfaces, the entire type hierarchy is searched. The method is deduced as
* follows.
* interfaces, the entire type hierarchy is searched. Alternatively, a factory
* method in an external class can be referenced via its fully-qualified method
* name following the syntax {@code <fully-qualified class name>#<method name>}
* &mdash; for example, {@code "org.example.TestUtils#createCustomerRepository"}.
*
* <p>The factory method is deduced as follows.
*
* <ul>
* <li>If the {@link #methodName()} is specified, look for a static method with
@@ -125,6 +129,10 @@ public @interface TestBean {
* <p>A search will be performed to find the factory method in the test class,
* in one of its superclasses, or in any implemented interfaces. In the case
* of a nested test class, the enclosing class hierarchy will also be searched.
* <p>Alternatively, a factory method in an external class can be referenced
* via its fully-qualified method name following the syntax
* {@code <fully-qualified class name>#<method name>} &mdash; for example,
* {@code "org.example.TestUtils#createCustomerRepository"}.
* <p>If left unspecified, the name of the factory method will be detected
* based either on the name of the annotated field or the name of the bean.
*/

View File

@@ -32,6 +32,8 @@ import org.springframework.core.ResolvableType;
import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.bean.override.BeanOverrideProcessor;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.MethodFilter;
import org.springframework.util.StringUtils;
@@ -109,12 +111,40 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
*/
Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, Collection<String> methodNames) {
Assert.notEmpty(methodNames, "At least one candidate method name is required");
Set<String> supportedNames = new LinkedHashSet<>(methodNames);
Set<Method> methods = new LinkedHashSet<>();
Set<String> originalNames = new LinkedHashSet<>(methodNames);
// Process fully-qualified method names first.
for (String methodName : methodNames) {
int indexOfHash = methodName.lastIndexOf('#');
if (indexOfHash != -1) {
String className = methodName.substring(0, indexOfHash).trim();
Assert.hasText(className, () -> "No class name present in fully-qualified method name: " + methodName);
String methodNameToUse = methodName.substring(indexOfHash + 1).trim();
Assert.hasText(methodNameToUse, () -> "No method name present in fully-qualified method name: " + methodName);
Class<?> declaringClass;
try {
declaringClass = ClassUtils.forName(className, getClass().getClassLoader());
}
catch (ClassNotFoundException | LinkageError ex) {
throw new IllegalStateException(
"Failed to load class for fully-qualified method name: " + methodName, ex);
}
Method externalMethod = ReflectionUtils.findMethod(declaringClass, methodNameToUse);
Assert.state(externalMethod != null && Modifier.isStatic(externalMethod.getModifiers()) &&
methodReturnType.isAssignableFrom(externalMethod.getReturnType()), () ->
"No static method found named %s in %s with return type %s".formatted(
methodNameToUse, className, methodReturnType.getName()));
methods.add(externalMethod);
originalNames.remove(methodName);
}
}
Set<String> supportedNames = new LinkedHashSet<>(originalNames);
MethodFilter methodFilter = method -> (Modifier.isStatic(method.getModifiers()) &&
supportedNames.contains(method.getName()) &&
methodReturnType.isAssignableFrom(method.getReturnType()));
Set<Method> methods = findMethods(clazz, methodFilter);
findMethods(methods, clazz, methodFilter);
String methodNamesDescription = supportedNames.stream()
.map(name -> name + "()").collect(Collectors.joining(" or "));
@@ -130,10 +160,10 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
return methods.iterator().next();
}
private static Set<Method> findMethods(Class<?> clazz, MethodFilter methodFilter) {
Set<Method> methods = MethodIntrospector.selectMethods(clazz, methodFilter);
private static Set<Method> findMethods(Set<Method> methods, Class<?> clazz, MethodFilter methodFilter) {
methods.addAll(MethodIntrospector.selectMethods(clazz, methodFilter));
if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
methods = findMethods(clazz.getEnclosingClass(), methodFilter);
findMethods(methods, clazz.getEnclosingClass(), methodFilter);
}
return methods;
}