Skip to content

Commit 8f81a12

Browse files
committed
Added actual overriding test to BeanMethodPolymorphismTests
Issue: SPR-10992
1 parent eed1a32 commit 8f81a12

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@
3636
*/
3737
public class BeanMethodPolymorphismTests {
3838

39+
@Test
40+
public void beanMethodDetectedOnSuperClass() {
41+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
42+
ctx.getBean("testBean", TestBean.class);
43+
}
44+
45+
@Test
46+
public void beanMethodOverriding() {
47+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
48+
ctx.register(OverridingConfig.class);
49+
ctx.setAllowBeanDefinitionOverriding(false);
50+
ctx.refresh();
51+
assertFalse(ctx.getDefaultListableBeanFactory().containsSingleton("testBean"));
52+
assertEquals("overridden", ctx.getBean("testBean", TestBean.class).toString());
53+
assertTrue(ctx.getDefaultListableBeanFactory().containsSingleton("testBean"));
54+
}
55+
3956
@Test
4057
public void beanMethodOverloadingWithoutInheritance() {
4158
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
@@ -112,15 +129,6 @@ public void beanMethodShadowing() {
112129
assertThat(ctx.getBean(String.class), equalTo("shadow"));
113130
}
114131

115-
@Test
116-
public void beanMethodsDetectedOnSuperClass() {
117-
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
118-
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(Config.class));
119-
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
120-
pp.postProcessBeanFactory(beanFactory);
121-
beanFactory.getBean("testBean", TestBean.class);
122-
}
123-
124132

125133
@Configuration
126134
static class BaseConfig {
@@ -129,7 +137,6 @@ static class BaseConfig {
129137
public TestBean testBean() {
130138
return new TestBean();
131139
}
132-
133140
}
134141

135142

@@ -138,6 +145,22 @@ static class Config extends BaseConfig {
138145
}
139146

140147

148+
@Configuration
149+
static class OverridingConfig extends BaseConfig {
150+
151+
@Bean @Lazy
152+
@Override
153+
public TestBean testBean() {
154+
return new TestBean() {
155+
@Override
156+
public String toString() {
157+
return "overridden";
158+
}
159+
};
160+
}
161+
}
162+
163+
141164
@Configuration
142165
static class ConfigWithOverloading {
143166

0 commit comments

Comments
 (0)