1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .springframework .context .ApplicationContext ;
25
25
import org .springframework .context .annotation .Bean ;
26
26
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 ;
29
27
import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
30
28
31
29
import static org .assertj .core .api .Assertions .assertThat ;
42
40
*/
43
41
public class TestBeanForInheritanceIntegrationTests {
44
42
45
- static Pojo enclosingClassBeanOverride () {
43
+ static Pojo enclosingClassBean () {
46
44
return new FakePojo ("in enclosing test class" );
47
45
}
48
46
49
47
@ SpringJUnitConfig
50
- public abstract static class AbstractTestBeanIntegrationTestCase {
48
+ abstract static class AbstractTestBeanIntegrationTestCase {
51
49
52
- @ TestBean ( name = "someBean" )
50
+ @ TestBean
53
51
Pojo someBean ;
54
52
55
- @ TestBean (name = "otherBean" )
53
+ @ TestBean ("otherBean" )
56
54
Pojo otherBean ;
57
55
58
- @ TestBean (name = "thirdBean" )
56
+ @ TestBean ("thirdBean" )
59
57
Pojo anotherBean ;
60
58
61
59
static Pojo otherBean () {
62
- return new FakePojo ("otherBean in superclass" );
60
+ return new FakePojo ("other in superclass" );
63
61
}
64
62
65
63
static Pojo thirdBean () {
66
64
return new FakePojo ("third in superclass" );
67
65
}
68
66
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" );
98
69
}
99
70
100
71
@ Configuration (proxyBeanMethods = false )
@@ -104,18 +75,22 @@ static class Config {
104
75
Pojo someBean () {
105
76
return new ProdPojo ();
106
77
}
78
+
107
79
@ Bean
108
80
Pojo otherBean () {
109
81
return new ProdPojo ();
110
82
}
83
+
111
84
@ Bean
112
85
Pojo thirdBean () {
113
86
return new ProdPojo ();
114
87
}
88
+
115
89
@ Bean
116
90
Pojo pojo () {
117
91
return new ProdPojo ();
118
92
}
93
+
119
94
@ Bean
120
95
Pojo pojo2 () {
121
96
return new ProdPojo ();
@@ -131,42 +106,72 @@ class NestedConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegration
131
106
@ Autowired
132
107
ApplicationContext ctx ;
133
108
134
- @ TestBean (name = "pojo" , methodName = "commonBeanOverride " )
109
+ @ TestBean (methodName = "commonBean " )
135
110
Pojo pojo ;
136
111
137
- @ TestBean (name = "pojo2" , methodName = "enclosingClassBeanOverride " )
112
+ @ TestBean (name = "pojo2" , methodName = "enclosingClassBean " )
138
113
Pojo pojo2 ;
139
114
140
115
static Pojo someBean () {
141
116
return new FakePojo ("someBeanOverride" );
142
117
}
143
118
144
- // Hides otherBean() defined in AbstractTestBeanIntegrationTestCase.
119
+ // "Overrides" otherBean() defined in AbstractTestBeanIntegrationTestCase.
145
120
static Pojo otherBean () {
146
- return new FakePojo ("otherBean in subclass" );
121
+ return new FakePojo ("other in subclass" );
147
122
}
148
123
149
124
@ 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" );
153
128
}
154
129
155
130
@ Test
156
- void fieldInSupertypeWithFactoryMethodInSubtype () {
131
+ void fieldInSuperclassWithFactoryMethodInSubclass () {
157
132
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" );
159
134
}
160
135
161
136
@ 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" );
165
140
}
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
+
166
148
@ Test
167
149
void fieldInNestedClassWithFactoryMethodInEnclosingClass () {
168
150
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 ();
170
175
}
171
176
}
172
177
0 commit comments