diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java
index edb843bde75..d4cc67bb7ad 100644
--- a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java
+++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java
@@ -36,9 +36,10 @@ import org.springframework.test.context.bean.override.BeanOverride;
*
*
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.
*
* - If the {@link #methodName()} is specified, look for a static method with
* that name.
diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java
index 5e56dfdf3ec..a70254b7ee0 100644
--- a/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java
+++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java
@@ -72,8 +72,8 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
* 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.
*
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 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 findMethods(Class> clazz, MethodFilter methodFilter) {
- return MethodIntrospector.selectMethods(clazz, methodFilter);
+ Set methods = MethodIntrospector.selectMethods(clazz, methodFilter);
+ if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
+ methods = findMethods(clazz.getEnclosingClass(), methodFilter);
+ }
+ return methods;
}
diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/MultipleNestingLevelsTestBeanIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/MultipleNestingLevelsTestBeanIntegrationTests.java
new file mode 100644
index 00000000000..275554282bb
--- /dev/null
+++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/MultipleNestingLevelsTestBeanIntegrationTests.java
@@ -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";
+ }
+ }
+
+}
diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanIntegrationTests.java
index 913e1971fae..a9cd9286ba5 100644
--- a/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanIntegrationTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanIntegrationTests.java
@@ -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");
+ }
+ }
}