/
SpringBeanScopesTest.java
193 lines (164 loc) · 7.97 KB
/
SpringBeanScopesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* SpringBeanScopesTest.java
*
* Created on 2017-11-15, 6:57
*/
package com.marcnuri.demo.springbeanscopes;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static com.marcnuri.demo.springbeanscopes.SampleAutowired.BEAN_NAME_FOR_AUTOWIRED_PROTOTYPE;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/**
* Created by Marc Nuri <marc@marcnuri.com> on 2017-11-15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes= {
SpringBeanScopesTest.class,
SingletonScopedController.class,
RequestScopedController.class,
SessionScopedController.class
})
@Configuration
@WebAppConfiguration
public class SpringBeanScopesTest {
//**************************************************************************************************
// Fields
//**************************************************************************************************
private static final String SINGLETON_BEAN_SAMPLE_NAME = "sample";
private static final String SINGLETON_ANNOTATED_BEAN_SAMPLE_NAME = "sampleAnnotated";
private static final String PROTOTYPE_BEAN_SAMPLE_NAME = "samplePrototype";
private static final String SINGLETON_BEAN_SAMPLE_AUTOWIRED_NAME = "sampleAutowired";
@Autowired
private ApplicationContext applicationContext;
@Autowired
private SingletonScopedController singletonScopedController;
@Autowired
private RequestScopedController requestScopedController;
@Autowired
private SessionScopedController sessionScopedController;
private MockMvc mockMvc;
//**************************************************************************************************
// Constructors
//**************************************************************************************************
//**************************************************************************************************
// Abstract Methods
//**************************************************************************************************
//**************************************************************************************************
// Overridden Methods
//**************************************************************************************************
//**************************************************************************************************
// Other Methods
//**************************************************************************************************
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders
.standaloneSetup(singletonScopedController, requestScopedController, sessionScopedController)
.build();
}
@After
public void tearDown() {
mockMvc = null;
}
@Bean(name = SINGLETON_BEAN_SAMPLE_NAME)
public Sample sample() {
return new Sample();
}
@Bean(name = SINGLETON_ANNOTATED_BEAN_SAMPLE_NAME)
@Scope(SCOPE_SINGLETON)
public Sample sampleAnnotated() {
return new Sample();
}
@Bean(name = PROTOTYPE_BEAN_SAMPLE_NAME)
@Scope(SCOPE_PROTOTYPE)
@Qualifier(BEAN_NAME_FOR_AUTOWIRED_PROTOTYPE)
public Sample samplePrototype() {
return new Sample();
}
@Bean(name = SINGLETON_BEAN_SAMPLE_AUTOWIRED_NAME)
@Scope(SCOPE_SINGLETON)
public SampleAutowired sampleAutowiredPrototype() {
return new SampleAutowired();
}
@Test
public void singletonTest_na_shouldBeSameInstance() {
Sample singleton1 = applicationContext.getBean(SINGLETON_BEAN_SAMPLE_NAME, Sample.class);
Sample singleton2 = applicationContext.getBean(SINGLETON_BEAN_SAMPLE_NAME, Sample.class);
Assert.assertEquals(singleton1, singleton2);
Sample singletonAnnotated1 = applicationContext.getBean(SINGLETON_ANNOTATED_BEAN_SAMPLE_NAME, Sample.class);
Sample singletonAnnotated2 = applicationContext.getBean(SINGLETON_ANNOTATED_BEAN_SAMPLE_NAME, Sample.class);
Assert.assertEquals(singletonAnnotated1, singletonAnnotated2);
Assert.assertNotEquals(singleton1, singletonAnnotated1);
}
@Test
public void singletonScopedController_na_shouldReturnSameValues() throws Exception {
String response1 = mockMvc.perform(get(AbstractController.SINGLETON_SCOPE_ENDPOINT))
.andReturn().getResponse().getContentAsString();
String response2 = mockMvc.perform(get(AbstractController.SINGLETON_SCOPE_ENDPOINT))
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(response1, response2);
}
@Test
public void prototypeTest_na_shouldBeDifferentInstance() {
Sample prototype1 = applicationContext.getBean(PROTOTYPE_BEAN_SAMPLE_NAME, Sample.class);
Sample prototype2 = applicationContext.getBean(PROTOTYPE_BEAN_SAMPLE_NAME, Sample.class);
Assert.assertNotEquals(prototype1, prototype2);
}
@Test
public void singletonWithAutowiredPrototype_na_shouldBeInstance() {
SampleAutowired singleton1 = applicationContext.getBean(SINGLETON_BEAN_SAMPLE_AUTOWIRED_NAME, SampleAutowired.class);
SampleAutowired singleton2 = applicationContext.getBean(SINGLETON_BEAN_SAMPLE_AUTOWIRED_NAME, SampleAutowired.class);
Assert.assertEquals(singleton1, singleton2);
Assert.assertEquals(singleton1.getSampleAutowiredPrototype(), singleton2.getSampleAutowiredPrototype());
}
@Test
public void requestScopedController_na_shouldReturnDifferentValues() throws Exception {
String response1 = mockMvc.perform(get(AbstractController.REQUEST_SCOPE_ENDPOINT))
.andReturn().getResponse().getContentAsString();
String response2 = mockMvc.perform(get(AbstractController.REQUEST_SCOPE_ENDPOINT))
.andReturn().getResponse().getContentAsString();
Assert.assertNotEquals(response1, response2);
}
@Test
public void sessionScopedController_na_shouldReturnSameValues() throws Exception {
MockHttpSession session1 = new MockHttpSession();
MockHttpSession session2 = new MockHttpSession();
String response1_1 = mockMvc.perform(get(AbstractController.SESSION_SCOPE_ENDPOINT).session(session1))
.andReturn().getResponse().getContentAsString();
String response1_2 = mockMvc.perform(get(AbstractController.SESSION_SCOPE_ENDPOINT).session(session1))
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(response1_1, response1_2);
String response2_1 = mockMvc.perform(get(AbstractController.SESSION_SCOPE_ENDPOINT).session(session2))
.andReturn().getResponse().getContentAsString();
String response2_2 = mockMvc.perform(get(AbstractController.SESSION_SCOPE_ENDPOINT).session(session2))
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(response2_1, response2_2);
Assert.assertNotEquals(response1_1, response2_1);
}
//**************************************************************************************************
// Getter/Setter Methods
//**************************************************************************************************
//**************************************************************************************************
// Static Methods
//**************************************************************************************************
//**************************************************************************************************
// Inner Classes
//**************************************************************************************************
}