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;
}

View File

@ -0,0 +1,131 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.convention;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@link TestBean @TestBean} integration tests with multiple levels of
* {@link Nested @Nested} test classes.
*
* @author Sam Brannen
* @since 6.2
*/
@SpringJUnitConfig
class MultipleNestingLevelsTestBeanIntegrationTests {
@TestBean(name = "field0", methodName = "testField0")
String field0;
static String testField0() {
return "zero";
}
static String testField1() {
return "one";
}
static String testField2() {
return "two";
}
@Test
void test() {
assertThat(field0).isEqualTo("zero");
}
@Nested
class NestedLevel1Tests {
@TestBean(name = "field1", methodName = "testField1")
String field1;
@Test
void test() {
assertThat(field0).isEqualTo("zero");
assertThat(field1).isEqualTo("one");
}
@Nested
class NestedLevel2Tests {
@TestBean(name = "field2", methodName = "testField2")
String field2;
@Test
void test() {
assertThat(field0).isEqualTo("zero");
assertThat(field1).isEqualTo("one");
assertThat(field2).isEqualTo("two");
}
@Nested
class NestedLevel3Tests {
@TestBean(name = "field3", methodName = "testField2")
String localField2;
// Local testField2() "hides" the method in the top-level enclosing class.
static String testField2() {
return "Local Two";
}
@Test
void test() {
assertThat(field0).isEqualTo("zero");
assertThat(field1).isEqualTo("one");
assertThat(field2).isEqualTo("two");
assertThat(localField2).isEqualTo("Local Two");
}
}
}
}
@Configuration
static class Config {
@Bean
String field0() {
return "replace me 0";
}
@Bean
String field1() {
return "replace me 1";
}
@Bean
String field2() {
return "replace me 2";
}
@Bean
String field3() {
return "replace me 3";
}
}
}

View File

@ -104,6 +104,29 @@ public class TestBeanIntegrationTests {
assertThat(ctx.getBean("methodRenamed2")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(TestBeanIntegrationTests.this.methodRenamed2).isEqualTo("nestedFieldOverride");
}
@Nested
@DisplayName("With @TestBean in the enclosing class of the enclosing class")
public class TestBeanFieldInEnclosingClassLevel2Tests {
@Test
void fieldHasOverride(ApplicationContext ctx) {
assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(TestBeanIntegrationTests.this.nestedField).isEqualTo("nestedFieldOverride");
}
@Test
void renamedFieldHasOverride(ApplicationContext ctx) {
assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(TestBeanIntegrationTests.this.renamed2).isEqualTo("nestedFieldOverride");
}
@Test
void fieldWithMethodNameHasOverride(ApplicationContext ctx) {
assertThat(ctx.getBean("methodRenamed2")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(TestBeanIntegrationTests.this.methodRenamed2).isEqualTo("nestedFieldOverride");
}
}
}
@Nested
@ -118,6 +141,20 @@ public class TestBeanIntegrationTests {
assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(this.nestedField2).isEqualTo("nestedFieldOverride");
}
@Nested
@DisplayName("With factory method in the enclosing class of the enclosing class")
public class TestBeanFactoryMethodInEnclosingClassLevel2Tests {
@TestBean(methodName = "nestedFieldTestOverride", name = "nestedField")
String nestedField2;
@Test
void fieldHasOverride(ApplicationContext ctx) {
assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride");
assertThat(this.nestedField2).isEqualTo("nestedFieldOverride");
}
}
}