1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNull;
5 import static org.junit.jupiter.api.Assertions.assertSame;
6 import static org.junit.jupiter.api.Assertions.fail;
7 import static org.junit.runners.MethodSorters.NAME_ASCENDING;
8
9 import javax.annotation.Resource;
10 import javax.inject.Inject;
11
12 import org.junit.FixMethodOrder;
13 import org.junit.Test;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.beans.factory.annotation.Value;
16
17
18
19
20 @FixMethodOrder(NAME_ASCENDING)
21 public final class TestedClassWithAnnotatedDITest {
22
23
24
25
26 static class TestedClass1 {
27
28
29 @Resource(name = "secondAction")
30 Runnable action2;
31
32
33 @Autowired
34 int someValue;
35
36
37 @Resource(name = "firstAction")
38 Runnable action1;
39
40
41 @Resource(name = "thirdAction")
42 Runnable action3;
43
44
45 @Inject
46 int anotherValue;
47
48
49
50 @Value("textValue")
51 String stringFieldWithValue;
52
53
54 @Value("123.45")
55 double numericFieldWithValue;
56
57
58 @Value("#{systemProperties.someProperty}")
59 String systemProperty;
60
61
62 @Value("${anotherSystemProperty}")
63 int anInt;
64
65
66 @Value("${propertyWithDefault:12345}")
67 long aLongValue;
68 }
69
70
71
72
73 static class TestedClass2 {
74
75
76 final int someValue;
77
78
79 final Runnable action;
80
81
82 @Resource
83 Runnable anotherAction;
84
85
86 String text;
87
88
89 @Inject
90 String anotherText;
91
92
93 @Autowired(required = false)
94 Runnable optionalAction;
95
96
97
98
99
100
101
102
103
104
105
106 @Autowired
107 TestedClass2(int someValue, Runnable action, String textValue) {
108 this.someValue = someValue;
109 this.action = action;
110 text = textValue;
111 }
112 }
113
114
115 @Tested
116 TestedClass1 tested1;
117
118
119 @Tested
120 TestedClass2 tested2;
121
122
123 @Injectable
124 Runnable firstAction;
125
126
127 @Injectable
128 final int someValue = 1;
129
130
131 @Injectable
132 Runnable action;
133
134
135 @Injectable
136 String textValue = "test";
137
138
139 @Injectable
140 String anotherText = "name2";
141
142
143 @Injectable
144 Runnable action3;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 @Test
169 public void injectAllAnnotatedInjectionPoints(@Injectable("2") int anotherValue, @Injectable Runnable secondAction,
170 @Injectable Runnable anotherAction, @Injectable("true") boolean unused,
171 @Injectable("test") String stringFieldWithValue, @Injectable("123.45") double numericFieldWithValue,
172 @Injectable("propertyValue") String systemProperty, @Injectable("123") int anInt,
173 @Injectable("987654") long aLong) {
174 assertSame(firstAction, tested1.action1);
175 assertSame(secondAction, tested1.action2);
176 assertSame(action3, tested1.action3);
177 assertEquals(1, tested1.someValue);
178 assertEquals(2, tested1.anotherValue);
179 assertEquals("test", tested1.stringFieldWithValue);
180 assertEquals(123.45, tested1.numericFieldWithValue, 0);
181 assertEquals("propertyValue", tested1.systemProperty);
182 assertEquals(123, tested1.anInt);
183 assertEquals(987654, tested1.aLongValue);
184
185 assertEquals(1, tested2.someValue);
186 assertSame(action, tested2.action);
187 assertSame(anotherAction, tested2.anotherAction);
188 assertSame(textValue, tested2.text);
189 assertSame(anotherText, tested2.anotherText);
190 assertNull(tested2.optionalAction);
191 }
192
193
194
195
196
197
198
199
200
201
202
203 @Test
204 public void leaveValueAnnotatedInjectionPointsWithDefaultInitializationValue(@Injectable Runnable action2,
205 @Injectable Runnable anotherAction, @Injectable("2") int anotherValue) {
206 assertNull(tested1.systemProperty);
207 assertEquals(0, tested1.anInt);
208 assertEquals(0, tested1.aLongValue);
209 }
210
211
212
213
214 @Test(expected = IllegalStateException.class)
215 public void failForAnnotatedFieldWhichLacksAnInjectable() {
216 fail("Must fail before starting");
217 }
218
219
220
221
222
223
224
225 @Test(expected = IllegalStateException.class)
226 public void failForAnnotatedFieldHavingAnInjectableOfTheSameTypeWhichWasAlreadyConsumed(
227 @Injectable Runnable secondAction) {
228 fail("Must fail before starting");
229 }
230 }