• 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 static org.assertj.core.api.Assertions.assertThat;
8 import static org.mockito.Mockito.when;
9 
10 import org.junit.Test;
11 import org.mockito.Mock;
12 import org.mockito.internal.debugging.InvocationsPrinter;
13 import org.mockitousage.IMethods;
14 import org.mockitoutil.TestBase;
15 
16 public class InvocationsPrinterTest extends TestBase {
17 
18     @Mock IMethods mock;
19 
20     @Test
no_invocations()21     public void no_invocations() {
22         assertThat(new InvocationsPrinter().printInvocations(mock))
23                 .isEqualTo("No interactions and stubbings found for mock: mock");
24     }
25 
26     @Test
prints_invocations()27     public void prints_invocations() {
28         mock.simpleMethod(100);
29         triggerInteraction();
30 
31         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
32                 .isEqualTo(
33                         filterLineNo(
34                                 "[Mockito] Interactions of: mock\n"
35                                         + " 1. mock.simpleMethod(100);\n"
36                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations(InvocationsPrinterTest.java:0)\n"
37                                         + " 2. mock.otherMethod();\n"
38                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:0)\n"));
39     }
40 
41     @Test
prints_stubbings()42     public void prints_stubbings() {
43         triggerStubbing();
44 
45         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
46                 .isEqualTo(
47                         filterLineNo(
48                                 "[Mockito] Unused stubbings of: mock\n"
49                                         + " 1. mock.simpleMethod(\"a\");\n"
50                                         + "  - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:70)\n"));
51     }
52 
53     @Test
prints_invocations_and_stubbings()54     public void prints_invocations_and_stubbings() {
55         triggerStubbing();
56 
57         mock.simpleMethod("a");
58         triggerInteraction();
59 
60         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
61                 .isEqualTo(
62                         filterLineNo(
63                                 "[Mockito] Interactions of: mock\n"
64                                         + " 1. mock.simpleMethod(\"a\");\n"
65                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations_and_stubbings(InvocationsPrinterTest.java:49)\n"
66                                         + "   - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:73)\n"
67                                         + " 2. mock.otherMethod();\n"
68                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:34)\n"));
69     }
70 
71     @Test
prints_invocations_and_unused_stubbings()72     public void prints_invocations_and_unused_stubbings() {
73         triggerStubbing();
74 
75         mock.simpleMethod("b");
76         triggerInteraction();
77 
78         assertThat(filterLineNo(new InvocationsPrinter().printInvocations(mock)))
79                 .isEqualTo(
80                         filterLineNo(
81                                 "[Mockito] Interactions of: mock\n"
82                                         + " 1. mock.simpleMethod(\"b\");\n"
83                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.prints_invocations_and_unused_stubbings(InvocationsPrinterTest.java:55)\n"
84                                         + " 2. mock.otherMethod();\n"
85                                         + "  -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerInteraction(InvocationsPrinterTest.java:34)\n"
86                                         + "[Mockito] Unused stubbings of: mock\n"
87                                         + " 1. mock.simpleMethod(\"a\");\n"
88                                         + "  - stubbed -> at org.mockitousage.debugging.InvocationsPrinterTest.triggerStubbing(InvocationsPrinterTest.java:62)\n"));
89     }
90 
triggerInteraction()91     private void triggerInteraction() {
92         mock.otherMethod();
93     }
94 
triggerStubbing()95     private void triggerStubbing() {
96         when(mock.simpleMethod("a")).thenReturn("x");
97     }
98 }
99