1 /* 2 * Copyright (c) 2007 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.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.mockito.internal.handler.NotifiedMethodInvocationReport; 11 import org.mockito.internal.invocation.InvocationBuilder; 12 import org.mockito.internal.invocation.StubInfoImpl; 13 import org.mockito.invocation.DescribedInvocation; 14 import org.mockito.invocation.Invocation; 15 16 import java.io.ByteArrayOutputStream; 17 import java.io.PrintStream; 18 19 import static org.assertj.core.api.Assertions.assertThat; 20 21 public class VerboseMockInvocationLoggerTest { 22 23 private VerboseMockInvocationLogger listener; 24 25 private ByteArrayOutputStream output; 26 private Invocation invocation = new InvocationBuilder().toInvocation(); 27 private DescribedInvocation stubbedInvocation = new InvocationBuilder().toInvocation(); 28 29 @Before init_Listener()30 public void init_Listener() throws Exception { 31 output = new ByteArrayOutputStream(); 32 listener = new VerboseMockInvocationLogger(new PrintStream(output)); 33 } 34 35 @After tearDown()36 public void tearDown() throws Exception { 37 System.out.println(output); 38 } 39 40 @Test should_print_to_system_out()41 public void should_print_to_system_out() { 42 assertThat(new VerboseMockInvocationLogger().printStream).isSameAs(System.out); 43 } 44 45 @Test should_print_invocation_with_return_value()46 public void should_print_invocation_with_return_value() { 47 // when 48 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, "return value")); 49 50 // then 51 assertThat(printed()) 52 .contains(invocation.toString()) 53 .contains(invocation.getLocation().toString()) 54 .contains("return value"); 55 } 56 57 @Test should_print_invocation_with_exception()58 public void should_print_invocation_with_exception() { 59 // when 60 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, new ThirdPartyException())); 61 62 // then 63 assertThat(printed()) 64 .contains(invocation.toString()) 65 .contains(invocation.getLocation().toString()) 66 .contains(ThirdPartyException.class.getName()); 67 } 68 69 @Test should_print_if_method_has_not_been_stubbed()70 public void should_print_if_method_has_not_been_stubbed() throws Exception { 71 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, "whatever")); 72 73 assertThat(printed()).doesNotContain("stubbed"); 74 } 75 76 @Test should_print_stubbed_info_if_availbable()77 public void should_print_stubbed_info_if_availbable() throws Exception { 78 invocation.markStubbed(new StubInfoImpl(stubbedInvocation)); 79 80 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, "whatever")); 81 82 assertThat(printed()) 83 .contains("stubbed") 84 .contains(stubbedInvocation.getLocation().toString()); 85 } 86 87 @Test should_log_count_of_interactions()88 public void should_log_count_of_interactions() { 89 // when & then 90 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, new ThirdPartyException())); 91 assertThat(printed()).contains("#1"); 92 93 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, new ThirdPartyException())); 94 assertThat(printed()).contains("#2"); 95 96 listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, new ThirdPartyException())); 97 assertThat(printed()).contains("#3"); 98 } 99 printed()100 private String printed() { 101 return output.toString(); 102 } 103 104 private static class ThirdPartyException extends Exception { 105 private static final long serialVersionUID = 3022739107688491354L; 106 } 107 } 108