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:
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user