|
| 1 | +package com.baeldung.scope; |
| 2 | + |
| 3 | +import com.baeldung.scope.prototype.PrototypeBean; |
| 4 | +import com.baeldung.scope.singletone.SingletonLookupBean; |
| 5 | +import com.baeldung.scope.singletone.SingletonObjectFactoryBean; |
| 6 | +import com.baeldung.scope.singletone.SingletonProviderBean; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 11 | +import org.springframework.context.support.AbstractApplicationContext; |
| 12 | +import org.springframework.test.context.ContextConfiguration; |
| 13 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 14 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 15 | + |
| 16 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 17 | +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class) |
| 18 | +public class PrototypeBeanInjectionTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void givenPrototypeInjection_WhenObjectFactory_ThenNewInstanceReturn() { |
| 22 | + |
| 23 | + AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); |
| 24 | + |
| 25 | + SingletonObjectFactoryBean firstContext = context.getBean(SingletonObjectFactoryBean.class); |
| 26 | + SingletonObjectFactoryBean secondContext = context.getBean(SingletonObjectFactoryBean.class); |
| 27 | + |
| 28 | + PrototypeBean firstInstance = firstContext.getPrototypeInstance(); |
| 29 | + PrototypeBean secondInstance = secondContext.getPrototypeInstance(); |
| 30 | + |
| 31 | + Assert.assertTrue("New instance expected", firstInstance != secondInstance); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenPrototypeInjection_WhenLookup_ThenNewInstanceReturn() { |
| 36 | + |
| 37 | + AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); |
| 38 | + |
| 39 | + SingletonLookupBean firstContext = context.getBean(SingletonLookupBean.class); |
| 40 | + SingletonLookupBean secondContext = context.getBean(SingletonLookupBean.class); |
| 41 | + |
| 42 | + PrototypeBean firstInstance = firstContext.getPrototypeBean(); |
| 43 | + PrototypeBean secondInstance = secondContext.getPrototypeBean(); |
| 44 | + |
| 45 | + Assert.assertTrue("New instance expected", firstInstance != secondInstance); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() { |
| 50 | + |
| 51 | + AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); |
| 52 | + |
| 53 | + SingletonProviderBean firstContext = context.getBean(SingletonProviderBean.class); |
| 54 | + SingletonProviderBean secondContext = context.getBean(SingletonProviderBean.class); |
| 55 | + |
| 56 | + PrototypeBean firstInstance = firstContext.getPrototypeInstance(); |
| 57 | + PrototypeBean secondInstance = secondContext.getPrototypeInstance(); |
| 58 | + |
| 59 | + Assert.assertTrue("New instance expected", firstInstance != secondInstance); |
| 60 | + } |
| 61 | +} |
0 commit comments