Skip to content

Commit 69ef885

Browse files
committed
Merge branch '6.2.x'
2 parents 57c0d95 + 019f764 commit 69ef885

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanForInheritanceIntegrationTests.java

+56-51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,6 @@
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.Configuration;
27-
import org.springframework.test.context.bean.override.convention.TestBeanForInheritanceIntegrationTests.AbstractTestBeanIntegrationTestCase.FakePojo;
28-
import org.springframework.test.context.bean.override.convention.TestBeanForInheritanceIntegrationTests.AbstractTestBeanIntegrationTestCase.Pojo;
2927
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3028

3129
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,59 +40,32 @@
4240
*/
4341
public class TestBeanForInheritanceIntegrationTests {
4442

45-
static Pojo enclosingClassBeanOverride() {
43+
static Pojo enclosingClassBean() {
4644
return new FakePojo("in enclosing test class");
4745
}
4846

4947
@SpringJUnitConfig
50-
public abstract static class AbstractTestBeanIntegrationTestCase {
48+
abstract static class AbstractTestBeanIntegrationTestCase {
5149

52-
@TestBean(name = "someBean")
50+
@TestBean
5351
Pojo someBean;
5452

55-
@TestBean(name = "otherBean")
53+
@TestBean("otherBean")
5654
Pojo otherBean;
5755

58-
@TestBean(name = "thirdBean")
56+
@TestBean("thirdBean")
5957
Pojo anotherBean;
6058

6159
static Pojo otherBean() {
62-
return new FakePojo("otherBean in superclass");
60+
return new FakePojo("other in superclass");
6361
}
6462

6563
static Pojo thirdBean() {
6664
return new FakePojo("third in superclass");
6765
}
6866

69-
static Pojo commonBeanOverride() {
70-
return new FakePojo("in superclass");
71-
}
72-
73-
public interface Pojo {
74-
75-
default String getValue() {
76-
return "Prod";
77-
}
78-
}
79-
80-
static class ProdPojo implements Pojo { }
81-
82-
static class FakePojo implements Pojo {
83-
final String value;
84-
85-
protected FakePojo(String value) {
86-
this.value = value;
87-
}
88-
89-
@Override
90-
public String getValue() {
91-
return this.value;
92-
}
93-
94-
@Override
95-
public String toString() {
96-
return getValue();
97-
}
67+
static Pojo commonBean() {
68+
return new FakePojo("common in superclass");
9869
}
9970

10071
@Configuration(proxyBeanMethods = false)
@@ -104,18 +75,22 @@ static class Config {
10475
Pojo someBean() {
10576
return new ProdPojo();
10677
}
78+
10779
@Bean
10880
Pojo otherBean() {
10981
return new ProdPojo();
11082
}
83+
11184
@Bean
11285
Pojo thirdBean() {
11386
return new ProdPojo();
11487
}
88+
11589
@Bean
11690
Pojo pojo() {
11791
return new ProdPojo();
11892
}
93+
11994
@Bean
12095
Pojo pojo2() {
12196
return new ProdPojo();
@@ -131,42 +106,72 @@ class NestedConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegration
131106
@Autowired
132107
ApplicationContext ctx;
133108

134-
@TestBean(name = "pojo", methodName = "commonBeanOverride")
109+
@TestBean(methodName = "commonBean")
135110
Pojo pojo;
136111

137-
@TestBean(name = "pojo2", methodName = "enclosingClassBeanOverride")
112+
@TestBean(name = "pojo2", methodName = "enclosingClassBean")
138113
Pojo pojo2;
139114

140115
static Pojo someBean() {
141116
return new FakePojo("someBeanOverride");
142117
}
143118

144-
// Hides otherBean() defined in AbstractTestBeanIntegrationTestCase.
119+
// "Overrides" otherBean() defined in AbstractTestBeanIntegrationTestCase.
145120
static Pojo otherBean() {
146-
return new FakePojo("otherBean in subclass");
121+
return new FakePojo("other in subclass");
147122
}
148123

149124
@Test
150-
void fieldInSubtypeWithFactoryMethodInSupertype() {
151-
assertThat(ctx.getBean("pojo")).as("applicationContext").hasToString("in superclass");
152-
assertThat(this.pojo.getValue()).as("injection point").isEqualTo("in superclass");
125+
void fieldInSuperclassWithFactoryMethodInSuperclass() {
126+
assertThat(ctx.getBean("thirdBean")).as("applicationContext").hasToString("third in superclass");
127+
assertThat(super.anotherBean.value()).as("injection point").isEqualTo("third in superclass");
153128
}
154129

155130
@Test
156-
void fieldInSupertypeWithFactoryMethodInSubtype() {
131+
void fieldInSuperclassWithFactoryMethodInSubclass() {
157132
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
158-
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
133+
assertThat(super.someBean.value()).as("injection point").isEqualTo("someBeanOverride");
159134
}
160135

161136
@Test
162-
void fieldInSupertypeWithPrioritizedFactoryMethodInSubtype() {
163-
assertThat(ctx.getBean("otherBean")).as("applicationContext").hasToString("otherBean in subclass");
164-
assertThat(super.otherBean.getValue()).as("injection point").isEqualTo("otherBean in subclass");
137+
void fieldInSuperclassWithFactoryMethodInSupeclassAndInSubclass() {
138+
assertThat(ctx.getBean("otherBean")).as("applicationContext").hasToString("other in subclass");
139+
assertThat(super.otherBean.value()).as("injection point").isEqualTo("other in subclass");
165140
}
141+
142+
@Test
143+
void fieldInSubclassWithFactoryMethodInSuperclass() {
144+
assertThat(ctx.getBean("pojo")).as("applicationContext").hasToString("common in superclass");
145+
assertThat(this.pojo.value()).as("injection point").isEqualTo("common in superclass");
146+
}
147+
166148
@Test
167149
void fieldInNestedClassWithFactoryMethodInEnclosingClass() {
168150
assertThat(ctx.getBean("pojo2")).as("applicationContext").hasToString("in enclosing test class");
169-
assertThat(this.pojo2.getValue()).as("injection point").isEqualTo("in enclosing test class");
151+
assertThat(this.pojo2.value()).as("injection point").isEqualTo("in enclosing test class");
152+
}
153+
}
154+
155+
interface Pojo {
156+
157+
default String value() {
158+
return "prod";
159+
}
160+
}
161+
162+
static class ProdPojo implements Pojo {
163+
164+
@Override
165+
public String toString() {
166+
return value();
167+
}
168+
}
169+
170+
record FakePojo(String value) implements Pojo {
171+
172+
@Override
173+
public String toString() {
174+
return value();
170175
}
171176
}
172177

0 commit comments

Comments
 (0)