View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNotSame;
5   
6   import java.util.concurrent.AbstractExecutorService;
7   
8   import javax.sql.DataSource;
9   
10  import mockit.internal.expectations.invocation.MissingInvocation;
11  
12  import org.junit.Rule;
13  import org.junit.Test;
14  import org.junit.rules.ExpectedException;
15  import org.w3c.dom.Attr;
16  
17  /**
18   * The Class MockInstanceMatchingTest.
19   */
20  public final class MockInstanceMatchingTest {
21  
22      /** The thrown. */
23      @Rule
24      public final ExpectedException thrown = ExpectedException.none();
25  
26      /**
27       * The Class Collaborator.
28       */
29      static class Collaborator {
30  
31          /** The value. */
32          private int value;
33  
34          /**
35           * Gets the value.
36           *
37           * @return the value
38           */
39          int getValue() {
40              return value;
41          }
42  
43          /**
44           * Sets the value.
45           *
46           * @param value
47           *            the new value
48           */
49          void setValue(int value) {
50              this.value = value;
51          }
52      }
53  
54      /** The mock. */
55      @Mocked
56      Collaborator mock;
57  
58      /**
59       * Match on mock instance.
60       *
61       * @param otherInstance
62       *            the other instance
63       */
64      @Test
65      public void matchOnMockInstance(@Mocked Collaborator otherInstance) {
66          new Expectations() {
67              {
68                  mock.getValue();
69                  result = 12;
70              }
71          };
72  
73          int result = mock.getValue();
74          assertEquals(12, result);
75  
76          Collaborator another = new Collaborator();
77          assertEquals(0, another.getValue());
78      }
79  
80      /**
81       * Record on mock instance but replay on different instance.
82       *
83       * @param verifiedMock
84       *            the verified mock
85       */
86      @Test
87      public void recordOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
88          thrown.expect(MissingInvocation.class);
89  
90          new Expectations() {
91              {
92                  verifiedMock.getValue();
93                  result = 12;
94              }
95          };
96  
97          Collaborator collaborator = new Collaborator();
98          assertEquals(0, collaborator.getValue());
99      }
100 
101     /**
102      * Verify expectation matching on mock instance.
103      *
104      * @param verifiedMock
105      *            the verified mock
106      */
107     @Test
108     public void verifyExpectationMatchingOnMockInstance(@Mocked final Collaborator verifiedMock) {
109         new Collaborator().setValue(12);
110         verifiedMock.setValue(12);
111 
112         new Verifications() {
113             {
114                 verifiedMock.setValue(12);
115                 times = 1;
116             }
117         };
118     }
119 
120     /**
121      * Verify expectations on same method call for different mocked instances.
122      *
123      * @param verifiedMock
124      *            the verified mock
125      */
126     @Test
127     public void verifyExpectationsOnSameMethodCallForDifferentMockedInstances(@Mocked final Collaborator verifiedMock) {
128         final Collaborator c1 = new Collaborator();
129         c1.getValue();
130         verifiedMock.getValue();
131         final Collaborator c2 = new Collaborator();
132         c2.getValue();
133 
134         new Verifications() {
135             {
136                 verifiedMock.getValue();
137                 times = 1;
138                 c1.getValue();
139                 times = 1;
140                 c2.getValue();
141                 times = 1;
142             }
143         };
144     }
145 
146     /**
147      * Verify on mock instance but replay on different instance.
148      *
149      * @param verifiedMock
150      *            the verified mock
151      */
152     @Test
153     public void verifyOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
154         thrown.expect(MissingInvocation.class);
155 
156         new Collaborator().setValue(12);
157 
158         new Verifications() {
159             {
160                 verifiedMock.setValue(12);
161             }
162         };
163     }
164 
165     /**
166      * Record expectations matching on multiple mock instances.
167      *
168      * @param mock2
169      *            the mock 2
170      */
171     @Test
172     public void recordExpectationsMatchingOnMultipleMockInstances(@Mocked final Collaborator mock2) {
173         new Expectations() {
174             {
175                 mock.getValue();
176                 result = 12;
177                 mock2.getValue();
178                 result = 13;
179                 mock.setValue(20);
180             }
181         };
182 
183         assertEquals(12, mock.getValue());
184         assertEquals(13, mock2.getValue());
185         mock.setValue(20);
186     }
187 
188     /**
189      * Record on specific mock instances but replay on different ones.
190      *
191      * @param mock2
192      *            the mock 2
193      */
194     @Test
195     public void recordOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
196         thrown.expect(MissingInvocation.class);
197 
198         new Expectations() {
199             {
200                 mock.setValue(12);
201                 mock2.setValue(13);
202             }
203         };
204 
205         mock2.setValue(12);
206         mock.setValue(13);
207     }
208 
209     /**
210      * Verify expectations matching on multiple mock instances.
211      *
212      * @param mock2
213      *            the mock 2
214      */
215     @Test
216     public void verifyExpectationsMatchingOnMultipleMockInstances(@Mocked final Collaborator mock2) {
217         mock.setValue(12);
218         mock2.setValue(13);
219         mock.setValue(20);
220 
221         new VerificationsInOrder() {
222             {
223                 mock.setValue(12);
224                 mock2.setValue(13);
225                 mock.setValue(20);
226             }
227         };
228     }
229 
230     /**
231      * Verify on specific mock instances but replay on different ones.
232      *
233      * @param mock2
234      *            the mock 2
235      */
236     @Test
237     public void verifyOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
238         thrown.expect(MissingInvocation.class);
239 
240         mock2.setValue(12);
241         mock.setValue(13);
242 
243         new FullVerifications() {
244             {
245                 mock.setValue(12);
246                 mock2.setValue(13);
247             }
248         };
249     }
250 
251     /**
252      * Match on two mock instances.
253      *
254      * @param mock2
255      *            the mock 2
256      */
257     @Test
258     public void matchOnTwoMockInstances(@Mocked final Collaborator mock2) {
259         new Expectations() {
260             {
261                 mock.getValue();
262                 result = 1;
263                 times = 1;
264                 mock2.getValue();
265                 result = 2;
266                 times = 1;
267             }
268         };
269 
270         assertEquals(1, mock.getValue());
271         assertEquals(2, mock2.getValue());
272     }
273 
274     /**
275      * Match on two mock instances and replay in different order.
276      *
277      * @param mock2
278      *            the mock 2
279      */
280     @Test
281     public void matchOnTwoMockInstancesAndReplayInDifferentOrder(@Mocked final Collaborator mock2) {
282         new Expectations() {
283             {
284                 mock.getValue();
285                 result = 1;
286                 mock2.getValue();
287                 result = 2;
288             }
289         };
290 
291         assertEquals(2, mock2.getValue());
292         assertEquals(1, mock.getValue());
293         assertEquals(1, mock.getValue());
294         assertEquals(2, mock2.getValue());
295     }
296 
297     /**
298      * Match on two mock instances for otherwise identical expectations.
299      *
300      * @param mock2
301      *            the mock 2
302      */
303     @Test
304     public void matchOnTwoMockInstancesForOtherwiseIdenticalExpectations(@Mocked final Collaborator mock2) {
305         mock.getValue();
306         mock2.getValue();
307         mock2.setValue(1);
308         mock.setValue(1);
309 
310         new Verifications() {
311             {
312                 mock.getValue();
313                 times = 1;
314                 mock2.getValue();
315                 times = 1;
316             }
317         };
318 
319         new VerificationsInOrder() {
320             {
321                 mock2.setValue(1);
322                 mock.setValue(1);
323             }
324         };
325     }
326 
327     /**
328      * Verify expectations matching on multiple mock parameters but replayed out of order.
329      *
330      * @param es1
331      *            the es 1
332      * @param es2
333      *            the es 2
334      */
335     @Test
336     public void verifyExpectationsMatchingOnMultipleMockParametersButReplayedOutOfOrder(
337             @Mocked final AbstractExecutorService es1, @Mocked final AbstractExecutorService es2) {
338         thrown.expect(MissingInvocation.class);
339 
340         es2.execute(null);
341         es1.submit((Runnable) null);
342 
343         new VerificationsInOrder() {
344             {
345                 es1.execute((Runnable) any);
346                 es2.submit((Runnable) any);
347             }
348         };
349     }
350 
351     /**
352      * Record expectation matching on instance created inside code under test.
353      */
354     @Test
355     public void recordExpectationMatchingOnInstanceCreatedInsideCodeUnderTest() {
356         new Expectations() {
357             {
358                 new Collaborator().getValue();
359                 result = 1;
360             }
361         };
362 
363         assertEquals(1, new Collaborator().getValue());
364     }
365 
366     /**
367      * Record expectations on two instances of same mocked interface.
368      *
369      * @param mockDS1
370      *            the mock DS 1
371      * @param mockDS2
372      *            the mock DS 2
373      * @param n
374      *            the n
375      *
376      * @throws Exception
377      *             the exception
378      */
379     @Test
380     public void recordExpectationsOnTwoInstancesOfSameMockedInterface(@Mocked final DataSource mockDS1,
381             @Mocked final DataSource mockDS2, @Mocked Attr n) throws Exception {
382         new Expectations() {
383             {
384                 mockDS1.getLoginTimeout();
385                 result = 1000;
386                 mockDS2.getLoginTimeout();
387                 result = 2000;
388             }
389         };
390 
391         assertNotSame(mockDS1, mockDS2);
392         assertEquals(1000, mockDS1.getLoginTimeout());
393         assertEquals(2000, mockDS2.getLoginTimeout());
394         mockDS2.setLoginTimeout(3000);
395 
396         new Verifications() {
397             {
398                 mockDS2.setLoginTimeout(anyInt);
399             }
400         };
401     }
402 
403     /**
404      * The Class BaseClass.
405      */
406     static class BaseClass {
407         /**
408          * Do something.
409          */
410         final void doSomething() {
411         }
412     }
413 
414     /**
415      * The Class SubclassA.
416      */
417     static final class SubclassA extends BaseClass {
418         /**
419          * Do something else.
420          */
421         void doSomethingElse() {
422         }
423     }
424 
425     /**
426      * The Class SubclassB.
427      */
428     static final class SubclassB extends BaseClass {
429         /**
430          * Do something else.
431          */
432         void doSomethingElse() {
433         }
434     }
435 
436     /**
437      * Verifying calls on specific instances of different subclasses.
438      *
439      * @param anyA
440      *            the any A
441      * @param a
442      *            the a
443      * @param anyB
444      *            the any B
445      */
446     @Test
447     public void verifyingCallsOnSpecificInstancesOfDifferentSubclasses(@Mocked SubclassA anyA,
448             @Mocked final SubclassA a, @Mocked final SubclassB anyB) {
449         a.doSomething();
450         new BaseClass().doSomething();
451         anyB.doSomething();
452         a.doSomethingElse();
453         new SubclassA().doSomethingElse();
454         anyB.doSomethingElse();
455 
456         new Verifications() {
457             {
458                 a.doSomethingElse();
459                 times = 1;
460                 anyB.doSomethingElse();
461                 times = 1;
462                 a.doSomething();
463                 times = 1;
464                 anyB.doSomething();
465                 times = 1;
466             }
467         };
468     }
469 }