View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit;
7   
8   import java.util.concurrent.AbstractExecutorService;
9   
10  import javax.sql.DataSource;
11  
12  import mockit.integration.junit5.ExpectedException;
13  import mockit.integration.junit5.JMockitExtension;
14  import mockit.internal.expectations.invocation.MissingInvocation;
15  import mockit.internal.expectations.invocation.UnexpectedInvocation;
16  
17  import org.junit.jupiter.api.Assertions;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  import org.w3c.dom.Attr;
21  
22  /**
23   * The Class MockInstanceMatchingTest.
24   */
25  @ExtendWith(JMockitExtension.class)
26  class MockInstanceMatchingTest {
27  
28      /**
29       * The Class Collaborator.
30       */
31      static class Collaborator {
32  
33          /** The value. */
34          private int value;
35  
36          /**
37           * Gets the value.
38           *
39           * @return the value
40           */
41          int getValue() {
42              return value;
43          }
44  
45          /**
46           * Sets the value.
47           *
48           * @param value
49           *            the new value
50           */
51          void setValue(int value) {
52              this.value = value;
53          }
54      }
55  
56      /** The mock. */
57      @Mocked
58      Collaborator mock;
59  
60      /**
61       * Match on mock instance.
62       *
63       * @param otherInstance
64       *            the other instance
65       */
66      @Test
67      void matchOnMockInstance(@Mocked Collaborator otherInstance) {
68          new Expectations() {
69              {
70                  mock.getValue();
71                  result = 12;
72              }
73          };
74  
75          int result = mock.getValue();
76          Assertions.assertEquals(12, result);
77  
78          Collaborator another = new Collaborator();
79          Assertions.assertEquals(0, another.getValue());
80      }
81  
82      /**
83       * Record on mock instance but replay on different instance.
84       *
85       * @param verifiedMock
86       *            the verified mock
87       */
88      @Test
89      @ExpectedException(MissingInvocation.class)
90      void recordOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
91          new Expectations() {
92              {
93                  verifiedMock.getValue();
94                  result = 12;
95              }
96          };
97  
98          Collaborator collaborator = new Collaborator();
99          Assertions.assertEquals(0, collaborator.getValue());
100     }
101 
102     /**
103      * Verify expectation matching on mock instance.
104      *
105      * @param verifiedMock
106      *            the verified mock
107      */
108     @Test
109     void verifyExpectationMatchingOnMockInstance(@Mocked final Collaborator verifiedMock) {
110         new Collaborator().setValue(12);
111         verifiedMock.setValue(12);
112 
113         new Verifications() {
114             {
115                 verifiedMock.setValue(12);
116                 times = 1;
117             }
118         };
119     }
120 
121     /**
122      * Verify expectations on same method call for different mocked instances.
123      *
124      * @param verifiedMock
125      *            the verified mock
126      */
127     @Test
128     void verifyExpectationsOnSameMethodCallForDifferentMockedInstances(@Mocked final Collaborator verifiedMock) {
129         final Collaborator c1 = new Collaborator();
130         c1.getValue();
131         verifiedMock.getValue();
132         final Collaborator c2 = new Collaborator();
133         c2.getValue();
134 
135         new Verifications() {
136             {
137                 verifiedMock.getValue();
138                 times = 1;
139                 c1.getValue();
140                 times = 1;
141                 c2.getValue();
142                 times = 1;
143             }
144         };
145     }
146 
147     /**
148      * Verify on mock instance but replay on different instance.
149      *
150      * @param verifiedMock
151      *            the verified mock
152      */
153     @Test
154     @ExpectedException(MissingInvocation.class)
155     void verifyOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
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     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         Assertions.assertEquals(12, mock.getValue());
184         Assertions.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     @ExpectedException(MissingInvocation.class)
196     void recordOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
197         new Expectations() {
198             {
199                 mock.setValue(12);
200                 mock2.setValue(13);
201             }
202         };
203 
204         mock2.setValue(12);
205         mock.setValue(13);
206     }
207 
208     /**
209      * Verify expectations matching on multiple mock instances.
210      *
211      * @param mock2
212      *            the mock 2
213      */
214     @Test
215     void verifyExpectationsMatchingOnMultipleMockInstances(@Mocked final Collaborator mock2) {
216         mock.setValue(12);
217         mock2.setValue(13);
218         mock.setValue(20);
219 
220         new VerificationsInOrder() {
221             {
222                 mock.setValue(12);
223                 mock2.setValue(13);
224                 mock.setValue(20);
225             }
226         };
227     }
228 
229     /**
230      * Verify on specific mock instances but replay on different ones.
231      *
232      * @param mock2
233      *            the mock 2
234      */
235     @Test
236     @ExpectedException(UnexpectedInvocation.class)
237     void verifyOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
238         mock2.setValue(12);
239         mock.setValue(13);
240 
241         Assertions.assertThrows(MissingInvocation.class, () -> {
242             new FullVerifications() {
243                 {
244                     mock.setValue(12);
245                     mock2.setValue(13);
246                 }
247             };
248         });
249     }
250 
251     /**
252      * Match on two mock instances.
253      *
254      * @param mock2
255      *            the mock 2
256      */
257     @Test
258     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         Assertions.assertEquals(1, mock.getValue());
271         Assertions.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     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         Assertions.assertEquals(2, mock2.getValue());
292         Assertions.assertEquals(1, mock.getValue());
293         Assertions.assertEquals(1, mock.getValue());
294         Assertions.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     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     @ExpectedException(MissingInvocation.class)
337     void verifyExpectationsMatchingOnMultipleMockParametersButReplayedOutOfOrder(
338             @Mocked final AbstractExecutorService es1, @Mocked final AbstractExecutorService es2) {
339         es2.execute(null);
340         es1.submit((Runnable) null);
341 
342         new VerificationsInOrder() {
343             {
344                 es1.execute((Runnable) any);
345                 es2.submit((Runnable) any);
346             }
347         };
348     }
349 
350     /**
351      * Record expectation matching on instance created inside code under test.
352      */
353     @Test
354     void recordExpectationMatchingOnInstanceCreatedInsideCodeUnderTest() {
355         new Expectations() {
356             {
357                 new Collaborator().getValue();
358                 result = 1;
359             }
360         };
361 
362         Assertions.assertEquals(1, new Collaborator().getValue());
363     }
364 
365     /**
366      * Record expectations on two instances of same mocked interface.
367      *
368      * @param mockDS1
369      *            the mock DS 1
370      * @param mockDS2
371      *            the mock DS 2
372      * @param n
373      *            the n
374      *
375      * @throws Exception
376      *             the exception
377      */
378     @Test
379     void recordExpectationsOnTwoInstancesOfSameMockedInterface(@Mocked final DataSource mockDS1,
380             @Mocked final DataSource mockDS2, @Mocked Attr n) throws Exception {
381         new Expectations() {
382             {
383                 mockDS1.getLoginTimeout();
384                 result = 1000;
385                 mockDS2.getLoginTimeout();
386                 result = 2000;
387             }
388         };
389 
390         Assertions.assertNotSame(mockDS1, mockDS2);
391         Assertions.assertEquals(1000, mockDS1.getLoginTimeout());
392         Assertions.assertEquals(2000, mockDS2.getLoginTimeout());
393         mockDS2.setLoginTimeout(3000);
394 
395         new Verifications() {
396             {
397                 mockDS2.setLoginTimeout(anyInt);
398             }
399         };
400     }
401 
402     /**
403      * The Class BaseClass.
404      */
405     static class BaseClass {
406         /**
407          * Do something.
408          */
409         final void doSomething() {
410         }
411     }
412 
413     /**
414      * The Class SubclassA.
415      */
416     static final class SubclassA extends BaseClass {
417         /**
418          * Do something else.
419          */
420         void doSomethingElse() {
421         }
422     }
423 
424     /**
425      * The Class SubclassB.
426      */
427     static final class SubclassB extends BaseClass {
428         /**
429          * Do something else.
430          */
431         void doSomethingElse() {
432         }
433     }
434 
435     /**
436      * Verifying calls on specific instances of different subclasses.
437      *
438      * @param anyA
439      *            the any A
440      * @param a
441      *            the a
442      * @param anyB
443      *            the any B
444      */
445     @Test
446     void verifyingCallsOnSpecificInstancesOfDifferentSubclasses(@Mocked SubclassA anyA, @Mocked final SubclassA a,
447             @Mocked final SubclassB anyB) {
448         a.doSomething();
449         new BaseClass().doSomething();
450         anyB.doSomething();
451         a.doSomethingElse();
452         new SubclassA().doSomethingElse();
453         anyB.doSomethingElse();
454 
455         new Verifications() {
456             {
457                 a.doSomethingElse();
458                 times = 1;
459                 anyB.doSomethingElse();
460                 times = 1;
461                 a.doSomething();
462                 times = 1;
463                 anyB.doSomething();
464                 times = 1;
465             }
466         };
467     }
468 }