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.handler; 6 7 import org.junit.Before; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.mockito.Mock; 11 import org.mockito.Spy; 12 import org.mockito.exceptions.base.MockitoException; 13 import org.mockito.internal.creation.MockSettingsImpl; 14 import org.mockito.invocation.Invocation; 15 import org.mockito.junit.MockitoJUnitRunner; 16 import org.mockito.listeners.InvocationListener; 17 import org.mockito.listeners.MethodInvocationReport; 18 import org.mockito.mock.MockCreationSettings; 19 import org.mockito.stubbing.Answer; 20 21 import java.text.ParseException; 22 import java.util.ArrayList; 23 24 import static org.assertj.core.api.Assertions.assertThat; 25 import static org.junit.Assert.fail; 26 import static org.mockito.BDDMockito.given; 27 import static org.mockito.BDDMockito.willThrow; 28 import static org.mockito.Matchers.any; 29 import static org.mockito.Mockito.mock; 30 import static org.mockito.Mockito.verify; 31 32 33 @RunWith(MockitoJUnitRunner.class) 34 @SuppressWarnings("unchecked") 35 public class InvocationNotifierHandlerTest { 36 private static final String SOME_LOCATION = "some location"; 37 private static final RuntimeException SOME_EXCEPTION = new RuntimeException(); 38 private static final OutOfMemoryError SOME_ERROR = new OutOfMemoryError(); 39 private static final Answer<?> SOME_ANSWER = mock(Answer.class); 40 41 42 @Mock private InvocationListener listener1; 43 @Mock private InvocationListener listener2; 44 @Spy private CustomListener customListener; 45 46 @Mock private Invocation invocation; 47 @Mock private MockHandlerImpl<ArrayList<Answer<?>>> mockHandler; 48 49 private InvocationNotifierHandler<ArrayList<Answer<?>>> notifier; 50 51 @Before 52 public void setUp() throws Exception { 53 notifier = new InvocationNotifierHandler<ArrayList<Answer<?>>>( 54 mockHandler, 55 (MockCreationSettings<ArrayList<Answer<?>>>) new MockSettingsImpl<ArrayList<Answer<?>>>().invocationListeners(customListener, listener1, listener2) 56 ); 57 } 58 59 @Test 60 public void should_notify_all_listeners_when_calling_delegate_handler() throws Throwable { 61 // given 62 given(mockHandler.handle(invocation)).willReturn("returned value"); 63 64 // when 65 notifier.handle(invocation); 66 67 // then 68 verify(listener1).reportInvocation(new NotifiedMethodInvocationReport(invocation, "returned value")); 69 verify(listener2).reportInvocation(new NotifiedMethodInvocationReport(invocation, "returned value")); 70 } 71 72 @Test 73 public void should_notify_all_listeners_when_called_delegate_handler_returns_ex() throws Throwable { 74 // given 75 Exception computedException = new Exception("computed"); 76 given(mockHandler.handle(invocation)).willReturn(computedException); 77 78 // when 79 notifier.handle(invocation); 80 81 // then 82 verify(listener1).reportInvocation(new NotifiedMethodInvocationReport(invocation, (Object) computedException)); 83 verify(listener2).reportInvocation(new NotifiedMethodInvocationReport(invocation, (Object) computedException)); 84 } 85 86 @Test(expected = ParseException.class) 87 public void should_notify_all_listeners_when_called_delegate_handler_throws_exception_and_rethrow_it() throws Throwable { 88 // given 89 ParseException parseException = new ParseException("", 0); 90 given(mockHandler.handle(invocation)).willThrow(parseException); 91 92 // when 93 try { 94 notifier.handle(invocation); 95 fail(); 96 } finally { 97 // then 98 verify(listener1).reportInvocation(new NotifiedMethodInvocationReport(invocation, parseException)); 99 verify(listener2).reportInvocation(new NotifiedMethodInvocationReport(invocation, parseException)); 100 } 101 } 102 103 @Test 104 public void should_report_listener_exception() throws Throwable { 105 willThrow(new NullPointerException()).given(customListener).reportInvocation(any(MethodInvocationReport.class)); 106 107 try { 108 notifier.handle(invocation); 109 fail(); 110 } catch (MockitoException me) { 111 assertThat(me.getMessage()) 112 .contains("invocation listener") 113 .contains("CustomListener") 114 .contains("threw an exception") 115 .contains("NullPointerException"); 116 } 117 } 118 119 @Test 120 public void should_delegate_all_MockHandlerInterface_to_the_parameterized_MockHandler() throws Exception { 121 notifier.getInvocationContainer(); 122 notifier.getMockSettings(); 123 124 verify(mockHandler).getInvocationContainer(); 125 verify(mockHandler).getMockSettings(); 126 } 127 128 private static class CustomListener implements InvocationListener { 129 public void reportInvocation(MethodInvocationReport methodInvocationReport) { 130 // nop 131 } 132 } 133 } 134