Browse Source

Test injection for @Lookup-declaring bean

Issue: SPR-14550
pull/1132/head
Juergen Hoeller 8 years ago
parent
commit
d128830605
  1. 24
      spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java

24
spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ public class LookupAnnotationTests {
aabpp.setBeanFactory(beanFactory); aabpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(aabpp); beanFactory.addBeanPostProcessor(aabpp);
beanFactory.registerBeanDefinition("abstractBean", new RootBeanDefinition(AbstractBean.class)); beanFactory.registerBeanDefinition("abstractBean", new RootBeanDefinition(AbstractBean.class));
beanFactory.registerBeanDefinition("beanConsumer", new RootBeanDefinition(BeanConsumer.class));
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
beanFactory.registerBeanDefinition("testBean", tbd); beanFactory.registerBeanDefinition("testBean", tbd);
@ -53,6 +54,7 @@ public class LookupAnnotationTests {
assertNotNull(bean); assertNotNull(bean);
Object expected = bean.get(); Object expected = bean.get();
assertEquals(TestBean.class, expected.getClass()); assertEquals(TestBean.class, expected.getClass());
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
} }
@Test @Test
@ -62,6 +64,7 @@ public class LookupAnnotationTests {
TestBean expected = bean.get("haha"); TestBean expected = bean.get("haha");
assertEquals(TestBean.class, expected.getClass()); assertEquals(TestBean.class, expected.getClass());
assertEquals("haha", expected.getName()); assertEquals("haha", expected.getName());
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
} }
@Test @Test
@ -71,6 +74,7 @@ public class LookupAnnotationTests {
TestBean expected = bean.getOneArgument("haha"); TestBean expected = bean.getOneArgument("haha");
assertEquals(TestBean.class, expected.getClass()); assertEquals(TestBean.class, expected.getClass());
assertEquals("haha", expected.getName()); assertEquals("haha", expected.getName());
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
} }
@Test @Test
@ -81,6 +85,7 @@ public class LookupAnnotationTests {
assertEquals(TestBean.class, expected.getClass()); assertEquals(TestBean.class, expected.getClass());
assertEquals("haha", expected.getName()); assertEquals("haha", expected.getName());
assertEquals(72, expected.getAge()); assertEquals(72, expected.getAge());
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
} }
@Test @Test
@ -93,6 +98,16 @@ public class LookupAnnotationTests {
} }
catch (AbstractMethodError ex) { catch (AbstractMethodError ex) {
} }
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
}
@Test
public void testWithEarlyInjection() {
AbstractBean bean = beanFactory.getBean("beanConsumer", BeanConsumer.class).abstractBean;
assertNotNull(bean);
Object expected = bean.get();
assertEquals(TestBean.class, expected.getClass());
assertSame(bean, beanFactory.getBean(BeanConsumer.class).abstractBean);
} }
@ -113,4 +128,11 @@ public class LookupAnnotationTests {
public abstract TestBean getThreeArguments(String name, int age, int anotherArg); public abstract TestBean getThreeArguments(String name, int age, int anotherArg);
} }
public static class BeanConsumer {
@Autowired
AbstractBean abstractBean;
}
} }

Loading…
Cancel
Save