• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.verification;
7 
8 import org.junit.Before;
9 import org.junit.Ignore;
10 import org.junit.Test;
11 import org.mockito.Mock;
12 import org.mockito.Mockito;
13 import org.mockito.exceptions.verification.NeverWantedButInvoked;
14 import org.mockito.exceptions.verification.NoInteractionsWanted;
15 import org.mockito.exceptions.verification.WantedButNotInvoked;
16 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
17 import org.mockitousage.IMethods;
18 import org.mockitoutil.TestBase;
19 
20 import static junit.framework.TestCase.fail;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.mockito.AdditionalMatchers.aryEq;
23 import static org.mockito.Mockito.*;
24 
25 public class DescriptiveMessagesWhenVerificationFailsTest extends TestBase {
26 
27     private IMethods mock;
28 
29     @Before
setup()30     public void setup() {
31         mock = Mockito.mock(IMethods.class, "iMethods");
32     }
33 
34     @Test
should_print_method_name()35     public void should_print_method_name() {
36         try {
37             verify(mock).simpleMethod();
38             fail();
39         } catch (WantedButNotInvoked e) {
40             String expectedMessage =
41                     "\n" +
42                     "Wanted but not invoked:" +
43                     "\n" +
44                     "iMethods.simpleMethod();" +
45                     "\n" +
46                     "-> at";
47             assertThat(e).hasMessageContaining(expectedMessage);
48         }
49     }
50 
51     private class Foo {
toString()52         public String toString() {
53             return "foo";
54         }
55     }
56 
57     @Test
should_print_method_name_and_arguments()58     public void should_print_method_name_and_arguments() {
59         try {
60             verify(mock).threeArgumentMethod(12, new Foo(), "xx");
61             fail();
62         } catch (WantedButNotInvoked e) {
63             assertThat(e).hasMessageContaining("iMethods.threeArgumentMethod(12, foo, \"xx\")");
64         }
65     }
66 
67     @Test
should_print_actual_and_wanted_in_line()68     public void should_print_actual_and_wanted_in_line() {
69         mock.varargs(1, 2);
70 
71         try {
72             verify(mock).varargs(1, 1000);
73             fail();
74         } catch (ArgumentsAreDifferent e) {
75             String wanted =
76                     "\n" +
77                     "Argument(s) are different! Wanted:" +
78                     "\n" +
79                     "iMethods.varargs(1, 1000);";
80 
81             assertThat(e).hasMessageContaining(wanted);
82 
83             String actual =
84                     "\n" +
85                     "Actual invocation has different arguments:" +
86                     "\n" +
87                     "iMethods.varargs(1, 2);";
88 
89             assertThat(e).hasMessageContaining(actual);
90         }
91     }
92 
93     @Test
should_print_actual_and_wanted_in_multiple_lines()94     public void should_print_actual_and_wanted_in_multiple_lines() {
95         mock.varargs("this is very long string", "this is another very long string");
96 
97         try {
98             verify(mock).varargs("x", "y", "z");
99             fail();
100         } catch (ArgumentsAreDifferent e) {
101             String wanted =
102                     "\n" +
103                     "Argument(s) are different! Wanted:" +
104                     "\n" +
105                     "iMethods.varargs(" +
106                     "\n" +
107                     "    \"x\"," +
108                     "\n" +
109                     "    \"y\"," +
110                     "\n" +
111                     "    \"z\"" +
112                     "\n" +
113                     ");";
114 
115             assertThat(e).hasMessageContaining(wanted);
116 
117             String actual =
118                     "\n" +
119                     "Actual invocation has different arguments:" +
120                     "\n" +
121                     "iMethods.varargs(" +
122                     "\n" +
123                     "    \"this is very long string\"," +
124                     "\n" +
125                     "    \"this is another very long string\"" +
126                     "\n" +
127                     ");";
128 
129             assertThat(e).hasMessageContaining(actual);
130         }
131     }
132 
133     @Test
should_print_actual_and_wanted_when_actual_method_name_and_wanted_method_name_are_the_same()134     public void should_print_actual_and_wanted_when_actual_method_name_and_wanted_method_name_are_the_same() {
135         mock.simpleMethod();
136 
137         try {
138             verify(mock).simpleMethod(10);
139             fail();
140         } catch (ArgumentsAreDifferent e) {
141             assertThat(e).hasMessageContaining("simpleMethod(10)").hasMessageContaining("simpleMethod()");
142         }
143     }
144 
145     @Test
should_print_actual_and_unverified_wanted_when_the_difference_is_about_arguments()146     public void should_print_actual_and_unverified_wanted_when_the_difference_is_about_arguments() {
147         mock.twoArgumentMethod(1, 1);
148         mock.twoArgumentMethod(2, 2);
149 
150         verify(mock).twoArgumentMethod(1, 1);
151         try {
152             verify(mock).twoArgumentMethod(2, 1000);
153             fail();
154         } catch (ArgumentsAreDifferent e) {
155             assertThat(e).hasMessageContaining("(2, 1000)").hasMessageContaining("(2, 2)");
156         }
157     }
158 
159     @Test
should_print_first_unexpected_invocation()160     public void should_print_first_unexpected_invocation() {
161         mock.oneArg(true);
162         mock.oneArg(false);
163         mock.threeArgumentMethod(1, "2", "3");
164 
165         verify(mock).oneArg(true);
166         try {
167             verifyNoMoreInteractions(mock);
168             fail();
169         } catch (NoInteractionsWanted e) {
170             String expectedMessage =
171                     "\n" +
172                     "No interactions wanted here:" +
173                     "\n" +
174                     "-> at";
175             assertThat(e).hasMessageContaining(expectedMessage);
176 
177             String expectedCause =
178                     "\n" +
179                     "But found this interaction on mock '" + mock + "':" +
180                     "\n" +
181                     "-> at";
182             assertThat(e).hasMessageContaining(expectedCause);
183         }
184     }
185 
186     @Test
should_print_first_unexpected_invocation_when_verifying_zero_interactions()187     public void should_print_first_unexpected_invocation_when_verifying_zero_interactions() {
188         mock.twoArgumentMethod(1, 2);
189         mock.threeArgumentMethod(1, "2", "3");
190 
191         try {
192             verifyZeroInteractions(mock);
193             fail();
194         } catch (NoInteractionsWanted e) {
195             String expected =
196                     "\n" +
197                     "No interactions wanted here:" +
198                     "\n" +
199                     "-> at";
200 
201             assertThat(e).hasMessageContaining(expected);
202 
203             String expectedCause =
204                 "\n" +
205                 "But found this interaction on mock '" + mock + "':" +
206                 "\n" +
207                 "-> at";
208 
209             assertThat(e).hasMessageContaining(expectedCause);
210         }
211     }
212 
213     @Test
should_print_method_name_when_verifying_at_least_once()214     public void should_print_method_name_when_verifying_at_least_once() throws Exception {
215         try {
216             verify(mock, atLeastOnce()).twoArgumentMethod(1, 2);
217             fail();
218         } catch (WantedButNotInvoked e) {
219             assertThat(e).hasMessageContaining("twoArgumentMethod(1, 2)");
220         }
221     }
222 
223     @Test
should_print_method_when_matcher_used()224     public void should_print_method_when_matcher_used() throws Exception {
225         try {
226             verify(mock, atLeastOnce()).twoArgumentMethod(anyInt(), eq(100));
227             fail();
228         } catch (WantedButNotInvoked e) {
229             String expectedMessage =
230                 "\n" +
231                 "Wanted but not invoked:" +
232                 "\n" +
233                 "iMethods.twoArgumentMethod(\n" +
234                 "    <any integer>,\n" +
235                 "    100\n" +
236                 ");";
237             assertThat(e).hasMessageContaining(expectedMessage);
238         }
239     }
240 
241     @Test
should_print_method_when_missing_invocation_with_array_matcher()242     public void should_print_method_when_missing_invocation_with_array_matcher() {
243         mock.oneArray(new boolean[] { true, false, false });
244 
245         try {
246             verify(mock).oneArray(aryEq(new boolean[] { false, false, false }));
247             fail();
248         } catch (ArgumentsAreDifferent e) {
249             assertThat(e)
250                 .hasMessageContaining("[false, false, false]")
251                 .hasMessageContaining("[true, false, false]");
252         }
253     }
254 
255     @Test
should_print_method_when_missing_invocation_with_vararg_matcher()256     public void should_print_method_when_missing_invocation_with_vararg_matcher() {
257         mock.varargsString(10, "xxx", "yyy", "zzz");
258 
259         try {
260             verify(mock).varargsString(10, "111", "222", "333");
261             fail();
262         } catch (ArgumentsAreDifferent e) {
263             assertThat(e)
264                 .hasMessageContaining("111")
265                 .hasMessageContaining("\"xxx\"");
266         }
267     }
268 
269     @Test
should_print_method_when_missing_invocation_with_matcher()270     public void should_print_method_when_missing_invocation_with_matcher() {
271         mock.simpleMethod("foo");
272 
273         try {
274             verify(mock).simpleMethod(matches("burrito from Exmouth"));
275             fail();
276         } catch (ArgumentsAreDifferent e) {
277             assertThat(e)
278                 .hasMessageContaining("matches(\"burrito from Exmouth\")")
279                 .hasMessageContaining("\"foo\"");
280         }
281     }
282 
283     @Test
should_print_null_arguments()284     public void should_print_null_arguments() throws Exception {
285         mock.simpleMethod(null, (Integer) null);
286         try {
287             verify(mock).simpleMethod("test");
288             fail();
289         } catch (ArgumentsAreDifferent e) {
290             assertThat(e).hasMessageContaining("simpleMethod(null, null);");
291         }
292     }
293 
294     @Test
should_say_never_wanted_but_invoked()295     public void should_say_never_wanted_but_invoked() throws Exception {
296         mock.simpleMethod(1);
297 
298         verify(mock, never()).simpleMethod(2);
299         try {
300             verify(mock, never()).simpleMethod(1);
301             fail();
302         } catch (NeverWantedButInvoked e) {
303             assertThat(e)
304                 .hasMessageContaining("Never wanted here:")
305                 .hasMessageContaining("But invoked here:");
306         }
307     }
308 
309     @Test
should_show_right_actual_method()310     public void should_show_right_actual_method() throws Exception {
311         mock.simpleMethod(9191);
312         mock.simpleMethod("foo");
313 
314         try {
315             verify(mock).simpleMethod("bar");
316             fail();
317         } catch (ArgumentsAreDifferent e) {
318             assertThat(e)
319                 .hasMessageContaining("bar")
320                 .hasMessageContaining("foo");
321         }
322     }
323 
324     @Mock private IMethods iHavefunkyName;
325 
326     @Test
should_print_field_name_when_annotations_used()327     public void should_print_field_name_when_annotations_used() throws Exception {
328         iHavefunkyName.simpleMethod(10);
329 
330         try {
331             verify(iHavefunkyName).simpleMethod(20);
332             fail();
333         } catch (ArgumentsAreDifferent e) {
334             assertThat(e)
335                 .hasMessageContaining("iHavefunkyName.simpleMethod(20)")
336                 .hasMessageContaining("iHavefunkyName.simpleMethod(10)");
337         }
338     }
339 
340     @Test
should_print_interactions_on_mock_when_ordinary_verification_fail()341     public void should_print_interactions_on_mock_when_ordinary_verification_fail() throws Exception {
342         mock.otherMethod();
343         mock.booleanReturningMethod();
344 
345         try {
346             verify(mock).simpleMethod();
347             fail();
348         } catch (WantedButNotInvoked e) {
349 //            assertContains("")
350         }
351     }
352 
353     @Mock private IMethods veeeeeeeeeeeeeeeeeeeeeeeerylongNameMock;
354 
355     @Test
should_never_break_method_string_when_no_args_in_method()356     public void should_never_break_method_string_when_no_args_in_method() throws Exception {
357         try {
358             verify(veeeeeeeeeeeeeeeeeeeeeeeerylongNameMock).simpleMethod();
359             fail();
360         } catch(WantedButNotInvoked e) {
361             assertThat(e).hasMessageContaining("veeeeeeeeeeeeeeeeeeeeeeeerylongNameMock.simpleMethod()");
362         }
363     }
364 
365     @Test
should_print_method_name_and_arguments_of_other_interactions_with_different_methods()366     public void should_print_method_name_and_arguments_of_other_interactions_with_different_methods() throws Exception {
367         try {
368             mock.arrayMethod(new String[] {"a", "b", "c"});
369             mock.forByte((byte) 25);
370 
371             verify(mock).threeArgumentMethod(12, new Foo(), "xx");
372             fail();
373         } catch (WantedButNotInvoked e) {
374             assertThat(e)
375                 .hasMessageContaining("iMethods.threeArgumentMethod(12, foo, \"xx\")")
376                 .hasMessageContaining("iMethods.arrayMethod([\"a\", \"b\", \"c\"])")
377                 .hasMessageContaining("iMethods.forByte((byte) 0x19)");
378         }
379     }
380 
381     @Test
382     @Ignore("issue 380 related")
should_print_method_name_and_arguments_of_other_interactions_of_same_method()383     public void should_print_method_name_and_arguments_of_other_interactions_of_same_method() throws Exception {
384         try {
385             mock.forByte((byte) 25);
386             mock.forByte((byte) 12);
387 
388             verify(mock).forByte((byte) 42);
389             fail();
390         } catch (WantedButNotInvoked e) {
391             assertThat(e)
392                 .hasMessageContaining("iMethods.forByte(42)")
393                 .hasMessageContaining("iMethods.forByte(25)")
394                 .hasMessageContaining("iMethods.forByte(12)");
395         }
396     }
397 
398     @Test
399     @Ignore("issue 380 related")
test1()400     public void test1() {
401         AnInterface m = Mockito.mock(AnInterface.class);
402 
403         for (int i = 1; i <= 2; i++) {
404             m.foo(i);
405         }
406 
407         verify(m).foo(1);
408         verify(m).foo(2);
409         verify(m).foo(3); // XXX: doesn't mention the parameters of foo(1) and foo(2)
410         verify(m).foo(4);
411     }
412 
413     @Test
414     @Ignore("issue 380 related")
test2()415     public void test2() {
416         AnInterface m = Mockito.mock(AnInterface.class);
417 
418         for (int i = 1; i <= 4; i++) {
419             m.foo(i);
420         }
421 
422         verify(m).foo(1);
423         verify(m).foo(2);
424         verify(m).foo(5); // XXX: doesn't mention foo(4) at all
425     }
426 
427     public interface AnInterface {
foo(int i)428         void foo(int i);
429     }
430 }
431