|
| 1 | +package org.baeldung.scopes; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | +import org.springframework.context.ApplicationContext; |
| 6 | +import org.springframework.context.support.AbstractApplicationContext; |
| 7 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 8 | + |
| 9 | +public class ScopesTest { |
| 10 | + |
| 11 | + private static final String NAME = "John Smith"; |
| 12 | + private static final int AGE = 30; |
| 13 | + |
| 14 | + private static final String NAME_OTHER = "Anna Jones"; |
| 15 | + private static final int AGE_OTHER = 40; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testScopeSingleton() { |
| 19 | + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml"); |
| 20 | + |
| 21 | + final Person personSingletonA = (Person) applicationContext.getBean("personSingleton"); |
| 22 | + final Person personSingletonB = (Person) applicationContext.getBean("personSingleton"); |
| 23 | + |
| 24 | + personSingletonA.setName(NAME); |
| 25 | + personSingletonB.setAge(AGE); |
| 26 | + |
| 27 | + Assert.assertEquals(NAME, personSingletonB.getName()); |
| 28 | + Assert.assertEquals(AGE, personSingletonB.getAge()); |
| 29 | + |
| 30 | + ((AbstractApplicationContext) applicationContext).close(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testScopePrototype() { |
| 35 | + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml"); |
| 36 | + |
| 37 | + final Person personPrototypeA = (Person) applicationContext.getBean("personPrototype"); |
| 38 | + final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype"); |
| 39 | + |
| 40 | + personPrototypeA.setName(NAME); |
| 41 | + personPrototypeA.setAge(AGE); |
| 42 | + |
| 43 | + personPrototypeB.setName(NAME_OTHER); |
| 44 | + personPrototypeB.setAge(AGE_OTHER); |
| 45 | + |
| 46 | + Assert.assertEquals(NAME, personPrototypeA.getName()); |
| 47 | + Assert.assertEquals(NAME_OTHER, personPrototypeB.getName()); |
| 48 | + |
| 49 | + ((AbstractApplicationContext) applicationContext).close(); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments