Find @⁠TestBean factory methods in multi-level @⁠Nested hierarchy

Prior to this commit, a @⁠TestBean factory method was found in the
directly enclosing class for a @⁠Nested test class; however, such a
factory method was not found in the enclosing class of the enclosing
class, etc.

This commit updates the search algorithm for @⁠TestBean factory methods
so that it recursively searches the enclosing class hierarchy for
@⁠Nested test classes.

Closes gh-32951
This commit is contained in:
Sam Brannen
2024-06-04 13:59:39 +02:00
parent f68130d2e5
commit 4d961fa472
4 changed files with 179 additions and 9 deletions

View File

@@ -36,9 +36,10 @@ import org.springframework.test.context.bean.override.BeanOverride;
*
* <p>The instance is created from a zero-argument static factory method in the
* test class whose return type is compatible with the annotated field. In the
* case of a nested test class, the enclosing class is also considered. Similarly,
* if the test class extends from a base class or implements any interfaces, the
* entire type hierarchy is considered. The method is deduced as follows.
* case of a nested test class, the enclosing class hierarchy is also considered.
* Similarly, if the test class extends from a base class or implements any
* interfaces, the entire type hierarchy is considered. The method is deduced as
* follows.
* <ul>
* <li>If the {@link #methodName()} is specified, look for a static method with
* that name.</li>

View File

@@ -72,8 +72,8 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
* <p>This method traverses up the type hierarchy of the given class in search
* of the factory method, beginning with the class itself and then searching
* implemented interfaces and superclasses. If a factory method is not found
* in the type hierarchy, this method will also search on the enclosing class
* if the class is a nested class.
* in the type hierarchy, this method will also search the enclosing class
* hierarchy if the class is a nested class.
* <p>If multiple factory methods are found that match the search criteria,
* an exception is thrown.
* @param clazz the class in which to search for the factory method
@@ -91,9 +91,6 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
methodReturnType.isAssignableFrom(method.getReturnType()));
Set<Method> methods = findMethods(clazz, methodFilter);
if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
methods = findMethods(clazz.getEnclosingClass(), methodFilter);
}
int methodCount = methods.size();
Assert.state(methodCount > 0, () -> """
@@ -139,7 +136,11 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
private static Set<Method> findMethods(Class<?> clazz, MethodFilter methodFilter) {
return MethodIntrospector.selectMethods(clazz, methodFilter);
Set<Method> methods = MethodIntrospector.selectMethods(clazz, methodFilter);
if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
methods = findMethods(clazz.getEnclosingClass(), methodFilter);
}
return methods;
}