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.mockito.internal.debugging; 7 8 import java.io.PrintStream; 9 import org.mockito.invocation.DescribedInvocation; 10 import org.mockito.listeners.InvocationListener; 11 import org.mockito.listeners.MethodInvocationReport; 12 13 /** 14 * Logs all invocations to standard output. 15 * 16 * Used for debugging interactions with a mock. 17 */ 18 public class VerboseMockInvocationLogger implements InvocationListener { 19 20 // visible for testing 21 final PrintStream printStream; 22 23 private int mockInvocationsCounter = 0; 24 VerboseMockInvocationLogger()25 public VerboseMockInvocationLogger() { 26 this(System.out); 27 } 28 VerboseMockInvocationLogger(PrintStream printStream)29 public VerboseMockInvocationLogger(PrintStream printStream) { 30 this.printStream = printStream; 31 } 32 reportInvocation(MethodInvocationReport methodInvocationReport)33 public void reportInvocation(MethodInvocationReport methodInvocationReport) { 34 printHeader(); 35 printStubInfo(methodInvocationReport); 36 printInvocation(methodInvocationReport.getInvocation()); 37 printReturnedValueOrThrowable(methodInvocationReport); 38 printFooter(); 39 } 40 printReturnedValueOrThrowable(MethodInvocationReport methodInvocationReport)41 private void printReturnedValueOrThrowable(MethodInvocationReport methodInvocationReport) { 42 if (methodInvocationReport.threwException()) { 43 String message = methodInvocationReport.getThrowable().getMessage() == null ? "" : " with message " + methodInvocationReport.getThrowable().getMessage(); 44 printlnIndented("has thrown: " + methodInvocationReport.getThrowable().getClass() + message); 45 } else { 46 String type = (methodInvocationReport.getReturnedValue() == null) ? "" : " (" + methodInvocationReport.getReturnedValue().getClass().getName() + ")"; 47 printlnIndented("has returned: \"" + methodInvocationReport.getReturnedValue() + "\"" + type); 48 } 49 } 50 printStubInfo(MethodInvocationReport methodInvocationReport)51 private void printStubInfo(MethodInvocationReport methodInvocationReport) { 52 if (methodInvocationReport.getLocationOfStubbing() != null) { 53 printlnIndented("stubbed: " + methodInvocationReport.getLocationOfStubbing()); 54 } 55 } 56 printHeader()57 private void printHeader() { 58 mockInvocationsCounter++; 59 printStream.println("############ Logging method invocation #" + mockInvocationsCounter + " on mock/spy ########"); 60 } 61 printInvocation(DescribedInvocation invocation)62 private void printInvocation(DescribedInvocation invocation) { 63 printStream.println(invocation.toString()); 64 // printStream.println("Handling method call on a mock/spy."); 65 printlnIndented("invoked: " + invocation.getLocation().toString()); 66 } 67 printFooter()68 private void printFooter() { 69 printStream.println(""); 70 } 71 printlnIndented(String message)72 private void printlnIndented(String message) { 73 printStream.println(" " + message); 74 } 75 76 } 77