1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 import static org.junit.jupiter.api.Assertions.assertNull;
6 import static org.junit.jupiter.api.Assertions.assertSame;
7
8 import java.lang.annotation.RetentionPolicy;
9 import java.util.concurrent.TimeUnit;
10
11 import org.junit.jupiter.api.Test;
12
13
14
15
16 final class MockedEnumsTest {
17
18
19
20
21 enum MyEnum {
22
23
24 First(true, 10, "First"),
25
26
27 Second(false, 6, "Second");
28
29
30 private final boolean flag;
31
32
33 private final int num;
34
35
36 private final String desc;
37
38
39
40
41
42
43
44
45
46
47
48 MyEnum(boolean flag, int num, String desc) {
49 this.flag = flag;
50 this.num = num;
51 this.desc = desc;
52 }
53
54
55
56
57
58
59
60
61
62 public double getValue(double f) {
63 return f * num;
64 }
65
66
67
68
69
70
71 public String getDescription() {
72 return num + desc + flag;
73 }
74 }
75
76
77
78
79
80
81
82 @Test
83 void oneEnumBeingMockedMustNotAffectOtherEnums(@Mocked MyEnum e) {
84 assertNotNull(RetentionPolicy.valueOf("RUNTIME"));
85 }
86
87
88
89
90
91
92
93 @Test
94 void mockEnumValues(@Mocked final MyEnum mock) {
95 new Expectations() {
96 {
97 MyEnum.values();
98 result = new MyEnum[] { mock };
99 mock.getValue(anyDouble);
100 result = 50.0;
101 }
102 };
103
104 MyEnum[] values = MyEnum.values();
105 assertEquals(1, values.length);
106
107 double value = values[0].getValue(0.5);
108 assertEquals(50.0, value, 0.0);
109 }
110
111
112
113
114
115
116
117 @Test
118 void mockInstanceMethodOnAnyEnumElement(@Mocked final MyEnum anyEnum) {
119 final double f = 2.5;
120
121 new Expectations() {
122 {
123 anyEnum.getValue(f);
124 result = 12.3;
125 }
126 };
127
128 assertEquals(12.3, MyEnum.First.getValue(f), 0.0);
129 assertEquals(12.3, MyEnum.Second.getValue(f), 0.0);
130 }
131
132
133
134
135
136
137
138 @Test
139 void verifyInstanceMethodInvocationOnAnyEnumElement(@Mocked MyEnum anyEnum) {
140 assertNull(MyEnum.First.getDescription());
141 assertNull(MyEnum.Second.getDescription());
142 assertNull(anyEnum.getDescription());
143
144 new Verifications() {
145 {
146 MyEnum.Second.getDescription();
147 times = 1;
148 }
149 };
150 }
151
152
153
154
155
156
157
158
159
160 @Test
161 void mockSpecificEnumElementsByUsingTwoMockInstances(@Mocked MyEnum mock1, @Mocked MyEnum mock2) {
162 new Expectations() {
163 {
164 MyEnum.First.getValue(anyDouble);
165 result = 12.3;
166 MyEnum.Second.getValue(anyDouble);
167 result = -5.01;
168 }
169 };
170
171 assertEquals(12.3, MyEnum.First.getValue(2.5), 0.0);
172 assertEquals(-5.01, MyEnum.Second.getValue(1), 0.0);
173 }
174
175
176
177
178
179
180
181 @Test
182 void mockSpecificEnumElementsEvenWhenUsingASingleMockInstance(@Mocked MyEnum unused) {
183 new Expectations() {
184 {
185 MyEnum.First.getValue(anyDouble);
186 result = 12.3;
187 MyEnum.Second.getValue(anyDouble);
188 result = -5.01;
189 }
190 };
191
192 assertEquals(-5.01, MyEnum.Second.getValue(1), 0.0);
193 assertEquals(12.3, MyEnum.First.getValue(2.5), 0.0);
194
195 new Verifications() {
196 {
197 MyEnum.First.getValue(2.5);
198 MyEnum.Second.getValue(1);
199 }
200 };
201 }
202
203
204
205
206
207
208
209
210
211
212 @Test
213 void mockNonAbstractMethodsInEnumWithAbstractMethod(@Mocked final TimeUnit tm) throws Exception {
214 new Expectations() {
215 {
216 tm.convert(anyLong, TimeUnit.SECONDS);
217 result = 1L;
218 tm.sleep(anyLong);
219 }
220 };
221
222 assertEquals(1, tm.convert(1000, TimeUnit.SECONDS));
223 tm.sleep(10000);
224 }
225
226
227
228
229 public enum EnumWithValueSpecificMethods {
230
231
232 One {
233 @Override
234 public int getValue() {
235 return 1;
236 }
237
238 @Override
239 public String getDescription() {
240 return "one";
241 }
242 },
243
244
245 Two {
246 @Override
247 public int getValue() {
248 return 2;
249 }
250
251 @Override
252 public String getDescription() {
253 return "two";
254 }
255 };
256
257
258
259
260
261
262 public abstract int getValue();
263
264
265
266
267
268
269 @SuppressWarnings("unused")
270 public String getDescription() {
271 return String.valueOf(getValue());
272 }
273 }
274
275
276
277
278
279
280
281 @Test
282 void mockEnumWithValueSpecificMethods(@Mocked EnumWithValueSpecificMethods mockedEnum) {
283 new Expectations() {
284 {
285 EnumWithValueSpecificMethods.One.getValue();
286 result = 123;
287 EnumWithValueSpecificMethods.Two.getValue();
288 result = -45;
289
290 EnumWithValueSpecificMethods.One.getDescription();
291 result = "1";
292 EnumWithValueSpecificMethods.Two.getDescription();
293 result = "2";
294 }
295 };
296
297 assertEquals(123, EnumWithValueSpecificMethods.One.getValue());
298 assertEquals(-45, EnumWithValueSpecificMethods.Two.getValue());
299 assertEquals("1", EnumWithValueSpecificMethods.One.getDescription());
300 assertEquals("2", EnumWithValueSpecificMethods.Two.getDescription());
301 }
302
303
304
305
306 enum Foo {
307
308 FOO;
309
310
311
312
313
314
315 String value() {
316 return "foo";
317 }
318 }
319
320
321
322
323 interface InterfaceWhichReturnsAnEnum {
324
325
326
327
328
329 Foo getFoo();
330 }
331
332
333
334
335
336
337
338 @Test
339 void cascadedEnum(@Mocked final InterfaceWhichReturnsAnEnum mock) {
340 final Foo foo = Foo.FOO;
341
342 new Expectations() {
343 {
344 mock.getFoo();
345 result = foo;
346 }
347 };
348
349 Foo cascadedFoo = mock.getFoo();
350 assertSame(foo, cascadedFoo);
351 assertEquals("foo", foo.value());
352 }
353 }