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