View Javadoc
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   * The Class MockedEnumsTest.
15   */
16  final class MockedEnumsTest {
17  
18      /**
19       * The Enum MyEnum.
20       */
21      enum MyEnum {
22  
23          /** The First. */
24          First(true, 10, "First"),
25  
26          /** The Second. */
27          Second(false, 6, "Second");
28  
29          /** The flag. */
30          private final boolean flag;
31  
32          /** The num. */
33          private final int num;
34  
35          /** The desc. */
36          private final String desc;
37  
38          /**
39           * Instantiates a new my enum.
40           *
41           * @param flag
42           *            the flag
43           * @param num
44           *            the num
45           * @param desc
46           *            the desc
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           * Gets the value.
56           *
57           * @param f
58           *            the f
59           *
60           * @return the value
61           */
62          public double getValue(double f) {
63              return f * num;
64          }
65  
66          /**
67           * Gets the description.
68           *
69           * @return the description
70           */
71          public String getDescription() {
72              return num + desc + flag;
73          }
74      }
75  
76      /**
77       * One enum being mocked must not affect other enums.
78       *
79       * @param e
80       *            the e
81       */
82      @Test
83      void oneEnumBeingMockedMustNotAffectOtherEnums(@Mocked MyEnum e) {
84          assertNotNull(RetentionPolicy.valueOf("RUNTIME"));
85      }
86  
87      /**
88       * Mock enum values.
89       *
90       * @param mock
91       *            the mock
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      * Mock instance method on any enum element.
113      *
114      * @param anyEnum
115      *            the any enum
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      * Verify instance method invocation on any enum element.
134      *
135      * @param anyEnum
136      *            the any enum
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      * Mock specific enum elements by using two mock instances.
154      *
155      * @param mock1
156      *            the mock 1
157      * @param mock2
158      *            the mock 2
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      * Mock specific enum elements even when using A single mock instance.
177      *
178      * @param unused
179      *            the unused
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      * Mock non abstract methods in enum with abstract method.
205      *
206      * @param tm
207      *            the tm
208      *
209      * @throws Exception
210      *             the exception
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      * The Enum EnumWithValueSpecificMethods.
228      */
229     public enum EnumWithValueSpecificMethods {
230 
231         /** The One. */
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         /** The Two. */
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          * Gets the value.
259          *
260          * @return the value
261          */
262         public abstract int getValue();
263 
264         /**
265          * Gets the description.
266          *
267          * @return the description
268          */
269         @SuppressWarnings("unused")
270         public String getDescription() {
271             return String.valueOf(getValue());
272         }
273     }
274 
275     /**
276      * Mock enum with value specific methods.
277      *
278      * @param mockedEnum
279      *            the mocked enum
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      * The Enum Foo.
305      */
306     enum Foo {
307         /** The foo. */
308         FOO;
309 
310         /**
311          * Value.
312          *
313          * @return the string
314          */
315         String value() {
316             return "foo";
317         }
318     }
319 
320     /**
321      * The Interface InterfaceWhichReturnsAnEnum.
322      */
323     interface InterfaceWhichReturnsAnEnum {
324         /**
325          * Gets the foo.
326          *
327          * @return the foo
328          */
329         Foo getFoo();
330     }
331 
332     /**
333      * Cascaded enum.
334      *
335      * @param mock
336      *            the mock
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 }