|
| 1 | +package com.baeldung.di.spring; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.springframework.context.ApplicationContext; |
| 7 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 8 | + |
| 9 | +public class BeanInjectionTest { |
| 10 | + |
| 11 | + private ApplicationContext applicationContext; |
| 12 | + |
| 13 | + @Before |
| 14 | + public void setUp() throws Exception { |
| 15 | + applicationContext = new ClassPathXmlApplicationContext("com.baeldung.di.spring.xml"); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void protoBean_getBean_returnsMultipleInstance() { |
| 20 | + final MessageApp messageApp1 = applicationContext.getBean("messageWorldApp", MessageApp.class); |
| 21 | + final MessageApp messageApp2 = applicationContext.getBean("messageWorldApp", MessageApp.class); |
| 22 | + assertNotEquals(messageApp1, messageApp2); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void protoFactoryMethod_getBean_returnsMultipleInstance() { |
| 27 | + final IndexApp indexApp1 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class); |
| 28 | + final IndexApp indexApp2 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class); |
| 29 | + assertNotEquals(indexApp1, indexApp2); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void protoStaticFactory_getBean_returnsMultipleInstance() { |
| 34 | + final IndexApp indexApp1 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class); |
| 35 | + final IndexApp indexApp2 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class); |
| 36 | + assertNotEquals(indexApp1, indexApp2); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void singletonBean_getBean_returnsSingleInstance() { |
| 41 | + final IndexApp indexApp1 = applicationContext.getBean("indexApp", IndexApp.class); |
| 42 | + final IndexApp indexApp2 = applicationContext.getBean("indexApp", IndexApp.class); |
| 43 | + assertEquals(indexApp1, indexApp2); |
| 44 | + } |
| 45 | +} |
0 commit comments