16
16
17
17
package org .springframework .context .annotation ;
18
18
19
- import static org .hamcrest .CoreMatchers .equalTo ;
20
- import static org .junit .Assert .assertThat ;
21
- import static org .junit .Assert .assertTrue ;
22
- import static org .junit .Assert .fail ;
23
-
24
19
import java .lang .annotation .Inherited ;
20
+ import java .util .List ;
25
21
22
+ import org .junit .Ignore ;
23
+ import org .junit .Rule ;
26
24
import org .junit .Test ;
25
+ import org .junit .rules .ExpectedException ;
27
26
import org .springframework .beans .factory .parsing .BeanDefinitionParsingException ;
28
27
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
29
28
import org .springframework .beans .factory .support .RootBeanDefinition ;
30
29
30
+ import static org .hamcrest .CoreMatchers .*;
31
+ import static org .junit .Assert .*;
32
+
31
33
/**
32
34
* Tests regarding overloading and overriding of bean methods.
33
35
* Related to SPR-6618.
42
44
* the most specific subclass bean method will always be the one that is invoked.
43
45
*
44
46
* @author Chris Beams
47
+ * @author Phillip Webb
45
48
*/
49
+ @ SuppressWarnings ("resource" )
46
50
public class BeanMethodPolymorphismTests {
47
51
52
+ @ Rule
53
+ public ExpectedException thrown = ExpectedException .none ();
54
+
55
+
48
56
@ Test
49
57
public void beanMethodOverloadingWithoutInheritance () {
58
+
50
59
@ SuppressWarnings ({ "hiding" })
51
60
@ Configuration class Config {
52
61
@ Bean String aString () { return "na" ; }
53
62
@ Bean String aString (Integer dependency ) { return "na" ; }
54
63
}
55
- try {
56
- new AnnotationConfigApplicationContext (Config .class );
57
- fail ("expected bean method overloading exception" );
58
- } catch (BeanDefinitionParsingException ex ) {
59
- assertTrue (ex .getMessage (), ex .getMessage ().contains ("2 overloaded @Bean methods named 'aString'" ));
60
- }
64
+
65
+ this .thrown .expect (BeanDefinitionParsingException .class );
66
+ this .thrown .expectMessage ("overloaded @Bean methods named 'aString'" );
67
+ new AnnotationConfigApplicationContext (Config .class );
61
68
}
62
69
63
70
@ Test
64
71
public void beanMethodOverloadingWithInheritance () {
65
72
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SubConfig .class );
66
73
assertThat (ctx .getBean (String .class ), equalTo ("overloaded5" ));
67
74
}
68
- static @ Configuration class SuperConfig {
69
- @ Bean String aString () { return "super" ; }
70
- }
71
- static @ Configuration class SubConfig {
72
- @ Bean Integer anInt () { return 5 ; }
73
- @ Bean String aString (Integer dependency ) { return "overloaded" +dependency ; }
75
+
76
+ @ Test
77
+ @ Ignore
78
+ public void beanMethodOverloadingWithInheritanceAndList () {
79
+ // SPR-11025
80
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SubConfigWithList .class );
81
+ assertThat (ctx .getBean (String .class ), equalTo ("overloaded5" ));
74
82
}
75
83
76
84
/**
@@ -83,10 +91,6 @@ public void beanMethodShadowing() {
83
91
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ShadowConfig .class );
84
92
assertThat (ctx .getBean (String .class ), equalTo ("shadow" ));
85
93
}
86
- @ Import (SubConfig .class )
87
- static @ Configuration class ShadowConfig {
88
- @ Bean String aString () { return "shadow" ; }
89
- }
90
94
91
95
/**
92
96
* Tests that polymorphic Configuration classes need not explicitly redeclare the
@@ -110,11 +114,63 @@ static class BaseConfig {
110
114
public TestBean testBean () {
111
115
return new TestBean ();
112
116
}
117
+
113
118
}
114
119
115
120
116
121
@ Configuration
117
122
static class Config extends BaseConfig {
118
123
}
119
124
125
+
126
+ @ Configuration
127
+ static class SuperConfig {
128
+
129
+ @ Bean
130
+ String aString () {
131
+ return "super" ;
132
+ }
133
+ }
134
+
135
+
136
+ @ Configuration
137
+ static class SubConfig extends SuperConfig {
138
+
139
+ @ Bean
140
+ Integer anInt () {
141
+ return 5 ;
142
+ }
143
+
144
+ @ Bean
145
+ String aString (Integer dependency ) {
146
+ return "overloaded" + dependency ;
147
+ }
148
+ }
149
+
150
+
151
+ @ Configuration
152
+ static class SubConfigWithList extends SuperConfig {
153
+
154
+ @ Bean
155
+ Integer anInt () {
156
+ return 5 ;
157
+ }
158
+
159
+ @ Bean
160
+ String aString (List <Integer > dependency ) {
161
+ return "overloaded" + dependency .get (0 );
162
+ }
163
+ }
164
+
165
+
166
+ @ Configuration
167
+ @ Import (SubConfig .class )
168
+ static class ShadowConfig {
169
+
170
+ @ Bean
171
+ String aString () {
172
+ return "shadow" ;
173
+ }
174
+ }
175
+
120
176
}
0 commit comments