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