1 /* 2 * Copyright (c) 2016 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.debugging; 6 7 import org.mockito.Mockito; 8 import org.mockito.internal.util.collections.ListUtil; 9 import org.mockito.invocation.Invocation; 10 import org.mockito.stubbing.Stubbing; 11 12 import java.util.Collection; 13 import java.util.LinkedList; 14 15 /** 16 * Prints invocations in human-readable, printable way 17 */ 18 public class InvocationsPrinter { 19 printInvocations(Object mock)20 public String printInvocations(Object mock) { 21 Collection<Invocation> invocations = Mockito.mockingDetails(mock).getInvocations(); 22 Collection<Stubbing> stubbings = Mockito.mockingDetails(mock).getStubbings(); 23 if (invocations.isEmpty() && stubbings.isEmpty()) { 24 return "No interactions and stubbings found for mock: " + mock; 25 } 26 27 StringBuilder sb = new StringBuilder(); 28 int x = 1; 29 for(Invocation i:invocations) { 30 if (x == 1) { 31 sb.append("[Mockito] Interactions of: ").append(mock).append("\n"); 32 } 33 sb.append(" ").append(x++).append(". ").append(i.toString()).append("\n"); 34 sb.append(" ").append(i.getLocation()).append("\n"); 35 if (i.stubInfo() != null) { 36 sb.append(" - stubbed ").append(i.stubInfo().stubbedAt()).append("\n"); 37 } 38 } 39 40 LinkedList<Stubbing> unused = ListUtil.filter(stubbings, new ListUtil.Filter<Stubbing>() { 41 public boolean isOut(Stubbing s) { 42 return s.wasUsed(); 43 } 44 }); 45 46 if (unused.isEmpty()) { 47 return sb.toString(); 48 } 49 sb.append("[Mockito] Unused stubbings of: " + mock).append("\n"); 50 51 x = 1; 52 for(Stubbing s:stubbings) { 53 sb.append(" ").append(x++).append(". ").append(s.getInvocation()).append("\n"); 54 sb.append(" - stubbed ").append(s.getInvocation().getLocation()).append("\n"); 55 } 56 return sb.toString(); 57 } 58 } 59