Skip to content

Commit 78c10cd

Browse files
author
Phillip Webb
committed
Polish BeanMethodPolymorphismTests
Polish BeanMethodPolymorphismTests and add @ignored failing test for SPR-10988. Issue: SPR-10988
1 parent 64869db commit 78c10cd

File tree

1 file changed

+77
-21
lines changed

1 file changed

+77
-21
lines changed

spring-context/src/test/java/org/springframework/context/annotation/BeanMethodPolymorphismTests.java

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616

1717
package org.springframework.context.annotation;
1818

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-
2419
import java.lang.annotation.Inherited;
20+
import java.util.List;
2521

22+
import org.junit.Ignore;
23+
import org.junit.Rule;
2624
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
2726
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
2827
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2928
import org.springframework.beans.factory.support.RootBeanDefinition;
3029

30+
import static org.hamcrest.CoreMatchers.*;
31+
import static org.junit.Assert.*;
32+
3133
/**
3234
* Tests regarding overloading and overriding of bean methods.
3335
* Related to SPR-6618.
@@ -42,35 +44,41 @@
4244
* the most specific subclass bean method will always be the one that is invoked.
4345
*
4446
* @author Chris Beams
47+
* @author Phillip Webb
4548
*/
49+
@SuppressWarnings("resource")
4650
public class BeanMethodPolymorphismTests {
4751

52+
@Rule
53+
public ExpectedException thrown = ExpectedException.none();
54+
55+
4856
@Test
4957
public void beanMethodOverloadingWithoutInheritance() {
58+
5059
@SuppressWarnings({ "hiding" })
5160
@Configuration class Config {
5261
@Bean String aString() { return "na"; }
5362
@Bean String aString(Integer dependency) { return "na"; }
5463
}
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);
6168
}
6269

6370
@Test
6471
public void beanMethodOverloadingWithInheritance() {
6572
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubConfig.class);
6673
assertThat(ctx.getBean(String.class), equalTo("overloaded5"));
6774
}
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"));
7482
}
7583

7684
/**
@@ -83,10 +91,6 @@ public void beanMethodShadowing() {
8391
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ShadowConfig.class);
8492
assertThat(ctx.getBean(String.class), equalTo("shadow"));
8593
}
86-
@Import(SubConfig.class)
87-
static @Configuration class ShadowConfig {
88-
@Bean String aString() { return "shadow"; }
89-
}
9094

9195
/**
9296
* Tests that polymorphic Configuration classes need not explicitly redeclare the
@@ -110,11 +114,63 @@ static class BaseConfig {
110114
public TestBean testBean() {
111115
return new TestBean();
112116
}
117+
113118
}
114119

115120

116121
@Configuration
117122
static class Config extends BaseConfig {
118123
}
119124

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+
120176
}

0 commit comments

Comments
 (0)