|
| 1 | +package com.baeldung.test.beaninjection; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.BeansException; |
| 9 | +import org.springframework.context.ApplicationContext; |
| 10 | +import org.springframework.context.ApplicationContextAware; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 13 | + |
| 14 | +import com.baeldung.beaninjection.AnotherSampleDAOBean; |
| 15 | +import com.baeldung.beaninjection.ExampleDAOBean; |
| 16 | +import com.baeldung.beaninjection.ExampleServiceBean; |
| 17 | +import com.baeldung.configuration.ApplicationContextTestBeanInjectionTypes; |
| 18 | + |
| 19 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 20 | +@ContextConfiguration(classes = ApplicationContextTestBeanInjectionTypes.class) |
| 21 | +public class BeanInjectionJavaConfigTest implements ApplicationContextAware { |
| 22 | + |
| 23 | + private ApplicationContext beanInjectedContext; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testDAOInjectionByJava() { |
| 27 | + ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class); |
| 28 | + assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getExampleDAO()); |
| 29 | + assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO()); |
| 30 | + assertTrue("Failed: Constructor Injection,String Property , Java Config", serviceBean.getPropertyX() |
| 31 | + .equals("Some Service Property X")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testPropertyInjectioninDAOByJava() { |
| 36 | + ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class); |
| 37 | + assertTrue("Failed: Constructor Injection,String Property , Java Config", daoBean.getPropertyX() |
| 38 | + .equals("Mandatory DAO Property X")); |
| 39 | + |
| 40 | + AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class); |
| 41 | + assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY() |
| 42 | + .equals("Mandatory DAO Property Y")); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 47 | + // TODO Auto-generated method stub |
| 48 | + this.beanInjectedContext = applicationContext; |
| 49 | + } |
| 50 | +} |
0 commit comments