• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.debugging;
6 
7 import org.junit.Test;
8 import org.mockito.Mock;
9 import org.mockito.internal.debugging.InvocationsPrinter;
10 import org.mockitousage.IMethods;
11 import org.mockitoutil.TestBase;
12 
13 import static org.assertj.core.api.Assertions.assertThat;
14 import static org.mockito.Mockito.when;
15 
16 public class InvocationsPrinterTest extends TestBase {
17 
18     @Mock IMethods mock;
19 
no_invocations()20     @Test public void no_invocations() {
21         assertThat(new InvocationsPrinter().printInvocations(mock)).isEqualTo("No interactions and stubbings found for mock: mock");
22     }
23 
prints_invocations()24     @Test public void prints_invocations() {
25         mock.simpleMethod(100);
26         triggerInteraction();
27 
28         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
29                 .isEqualTo(filterLineNo("[Mockito] Interactions of: mock\n" +
30                         " 1. mock.simpleMethod(100);\n" +
31                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations(InvocationsPrinterTest.java:0)\n" +
32                         " 2. mock.otherMethod();\n" +
33                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:0)\n"));
34     }
35 
prints_stubbings()36     @Test public void prints_stubbings() {
37         triggerStubbing();
38 
39         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
40                 .isEqualTo(filterLineNo("[Mockito] Unused stubbings of: mock\n" +
41                         " 1. mock.simpleMethod(\"a\");\n" +
42                         "  - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:70)\n"));
43     }
44 
prints_invocations_and_stubbings()45     @Test public void prints_invocations_and_stubbings() {
46         triggerStubbing();
47 
48         mock.simpleMethod("a");
49         triggerInteraction();
50 
51         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
52                 .isEqualTo(filterLineNo("[Mockito] Interactions of: mock\n" +
53                         " 1. mock.simpleMethod(\"a\");\n" +
54                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations_and_stubbings(InvocationsPrinterTest.java:49)\n" +
55                         "   - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:73)\n" +
56                         " 2. mock.otherMethod();\n" +
57                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:34)\n"));
58     }
59 
prints_invocations_and_unused_stubbings()60     @Test public void prints_invocations_and_unused_stubbings() {
61         triggerStubbing();
62 
63         mock.simpleMethod("b");
64         triggerInteraction();
65 
66         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
67                 .isEqualTo(filterLineNo("[Mockito] Interactions of: mock\n" +
68                         " 1. mock.simpleMethod(\"b\");\n" +
69                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations_and_unused_stubbings(InvocationsPrinterTest.java:55)\n" +
70                         " 2. mock.otherMethod();\n" +
71                         "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:34)\n" +
72                         "[Mockito] Unused stubbings of: mock\n" +
73                         " 1. mock.simpleMethod(\"a\");\n" +
74                         "  - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:62)\n"));
75     }
76 
triggerInteraction()77     private void triggerInteraction() {
78         mock.otherMethod();
79     }
80 
triggerStubbing()81     private void triggerStubbing() {
82         when(mock.simpleMethod("a")).thenReturn("x");
83     }
84 }
85