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 mockit.integration.junit5.ExpectedException;
9   import mockit.integration.junit5.JMockitExtension;
10  import mockit.internal.expectations.invocation.MissingInvocation;
11  import mockit.internal.expectations.invocation.UnexpectedInvocation;
12  
13  import org.junit.jupiter.api.Assertions;
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.api.extension.ExtendWith;
16  
17  /**
18   * The Class AssertionErrorMessagesTest.
19   */
20  @ExtendWith(JMockitExtension.class)
21  class AssertionErrorMessagesTest {
22  
23      /**
24       * The Class Collaborator.
25       */
26      static class Collaborator {
27  
28          /**
29           * Do something.
30           */
31          void doSomething() {
32              // do nothing
33          }
34  
35          /**
36           * Do something.
37           *
38           * @param i
39           *            the i
40           * @param s
41           *            the s
42           */
43          void doSomething(@SuppressWarnings("unused") int i, @SuppressWarnings("unused") String s) {
44              // do nothing
45          }
46  
47          /**
48           * Do something else.
49           *
50           * @param s
51           *            the s
52           */
53          void doSomethingElse(@SuppressWarnings("unused") String s) {
54              // do nothing
55          }
56      }
57  
58      /** The mock. */
59      @Mocked
60      Collaborator mock;
61  
62      /**
63       * Unexpected invocation for recorded expectation.
64       */
65      @Test
66      void unexpectedInvocationForRecordedExpectation() {
67          new Expectations() {
68              {
69                  mock.doSomething(anyInt, anyString);
70                  times = 1;
71              }
72          };
73  
74          mock.doSomething(1, "Abc");
75          Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> mock.doSomething(2, "xyz"));
76          Assertions.assertTrue(exception.getMessage().contains("Unexpected invocation to"));
77          Assertions.assertTrue(exception.getMessage().contains("doSomething(2, \"xyz\""));
78      }
79  
80      /**
81       * Unexpected invocation where expecting another for recorded expectations.
82       */
83      @Test
84      void unexpectedInvocationWhereExpectingAnotherForRecordedExpectations() {
85          mock.doSomething(1, "Abc");
86          mock.doSomething(2, "xyz");
87          mock.doSomethingElse("test");
88  
89          Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
90              new VerificationsInOrder() {
91                  {
92                      mock.doSomething(anyInt, anyString);
93                      times = 1;
94                      mock.doSomethingElse(anyString);
95                  }
96              };
97          });
98          Assertions.assertTrue(exception.getMessage().contains("doSomething(2, \"xyz\""));
99      }
100 
101     /**
102      * Unexpected invocation for recorded expectation with maximum invocation count of zero.
103      */
104     @Test
105     void unexpectedInvocationForRecordedExpectationWithMaximumInvocationCountOfZero() {
106         new Expectations() {
107             {
108                 mock.doSomething(anyInt, anyString);
109                 times = 0;
110             }
111         };
112 
113         Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> mock.doSomething(1, "Abc"));
114         Assertions.assertTrue(exception.getMessage().contains("1, \"Abc\""));
115     }
116 
117     /**
118      * Unexpected invocation for verified expectation.
119      */
120     @Test
121     void unexpectedInvocationForVerifiedExpectation() {
122         mock.doSomething(123, "Test");
123         mock.doSomethingElse("abc");
124 
125         Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
126             new Verifications() {
127                 {
128                     mock.doSomething(123, anyString);
129                     times = 0;
130                 }
131             };
132         });
133         Assertions.assertTrue(exception.getMessage().contains("123, \"Test\""));
134     }
135 
136     /**
137      * Unexpected invocation for expectations verified in order.
138      */
139     @Test
140     void unexpectedInvocationForExpectationsVerifiedInOrder() {
141         mock.doSomethingElse("test");
142         mock.doSomething(123, "Test");
143 
144         Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
145             new VerificationsInOrder() {
146                 {
147                     mock.doSomethingElse(anyString);
148                     mock.doSomething(anyInt, anyString);
149                     times = 0;
150                 }
151             };
152         });
153         Assertions.assertTrue(exception.getMessage().contains("123, \"Test\""));
154     }
155 
156     /**
157      * Unexpected invocation on method with no parameters.
158      */
159     @Test
160     @ExpectedException(value = MissingInvocation.class)
161     void unexpectedInvocationOnMethodWithNoParameters() {
162         new Expectations() {
163             {
164                 mock.doSomethingElse(anyString);
165             }
166         };
167 
168         mock.doSomething();
169 
170         Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
171             new FullVerifications(mock) {
172             };
173         });
174         Assertions.assertTrue(exception.getMessage().contains("doSomething()\n   on mock instance"));
175     }
176 
177     /**
178      * Missing invocation for recorded expectation.
179      */
180     @Test
181     @ExpectedException(value = MissingInvocation.class, expectedMessages = "any int, any String")
182     void missingInvocationForRecordedExpectation() {
183         new Expectations() {
184             {
185                 mock.doSomething(anyInt, anyString);
186                 times = 2;
187             }
188         };
189 
190         mock.doSomething(123, "Abc");
191     }
192 
193     /**
194      * Missing invocation for recorded expectation which gets non matching invocations at replay time.
195      */
196     @Test
197     @ExpectedException(value = MissingInvocation.class, expectedMessages = { "doSomethingElse(\"test\")",
198             "instead got:", "doSomethingElse(\"Abc\")", "doSomethingElse(\"\")" })
199     void missingInvocationForRecordedExpectationWhichGetsNonMatchingInvocationsAtReplayTime() {
200         new Expectations() {
201             {
202                 mock.doSomethingElse("test");
203             }
204         };
205 
206         mock.doSomethingElse("Abc");
207         mock.doSomething(1, "xy");
208         mock.doSomethingElse("");
209     }
210 
211     /**
212      * Missing invocation for verified expectation.
213      */
214     @Test
215     void missingInvocationForVerifiedExpectation() {
216         Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
217             new Verifications() {
218                 {
219                     mock.doSomething(123, anyString);
220                 }
221             };
222         });
223         Assertions.assertTrue(exception.getMessage().contains("123, any String"));
224     }
225 
226     /**
227      * Missing invocation for verified expectation which gets non matching invocations at replay time.
228      */
229     @Test
230     void missingInvocationForVerifiedExpectationWhichGetsNonMatchingInvocationsAtReplayTime() {
231         mock.doSomethingElse("Abc");
232         mock.doSomething(1, "xy");
233         mock.doSomethingElse("");
234 
235         Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
236             new Verifications() {
237                 {
238                     mock.doSomethingElse("test");
239                 }
240             };
241         });
242         Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"test\")"));
243         Assertions.assertTrue(exception.getMessage().contains("instead got:"));
244         Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"Abc\")"));
245         Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"\")"));
246     }
247 
248     /**
249      * Missing invocation for expectation verified in order.
250      */
251     @Test
252     void missingInvocationForExpectationVerifiedInOrder() {
253         mock.doSomething(123, "Test");
254 
255         Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
256             new VerificationsInOrder() {
257                 {
258                     mock.doSomething(anyInt, anyString);
259                     minTimes = 3;
260                 }
261             };
262         });
263         Assertions.assertTrue(exception.getMessage().contains("any int, any String"));
264     }
265 
266     /**
267      * Missing invocation for fully verified expectations.
268      */
269     @Test
270     void missingInvocationForFullyVerifiedExpectations() {
271         mock.doSomething(123, "Abc");
272 
273         Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
274             new FullVerifications() {
275                 {
276                     mock.doSomething(anyInt, anyString);
277                     times = 2;
278                 }
279             };
280         });
281         Assertions.assertTrue(exception.getMessage().contains("any int, any String"));
282     }
283 
284     /**
285      * Missing invocation for expectation using matcher for different parameter type.
286      */
287     @Test
288     void missingInvocationForExpectationUsingMatcherForDifferentParameterType() {
289         mock.doSomething(5, "");
290 
291         Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
292             new Verifications() {
293                 {
294                     mock.doSomething(anyChar, "");
295                 }
296             };
297         });
298         Assertions.assertTrue(exception.getMessage().contains("any char"));
299     }
300 }