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.handler; 7 8 import org.junit.Test; 9 import org.mockito.exceptions.base.MockitoException; 10 import org.mockito.exceptions.misusing.InvalidUseOfMatchersException; 11 import org.mockito.exceptions.misusing.WrongTypeOfReturnValue; 12 import org.mockito.internal.creation.MockSettingsImpl; 13 import org.mockito.internal.invocation.InvocationBuilder; 14 import org.mockito.internal.invocation.InvocationImpl; 15 import org.mockito.internal.invocation.InvocationMatcher; 16 import org.mockito.internal.invocation.MatchersBinder; 17 import org.mockito.internal.progress.ArgumentMatcherStorage; 18 import org.mockito.internal.stubbing.InvocationContainerImpl; 19 import org.mockito.internal.stubbing.StubbedInvocationMatcher; 20 import org.mockito.internal.stubbing.answers.Returns; 21 import org.mockito.internal.verification.VerificationModeFactory; 22 import org.mockito.invocation.Invocation; 23 import org.mockito.listeners.InvocationListener; 24 import org.mockito.listeners.MethodInvocationReport; 25 import org.mockitoutil.TestBase; 26 27 import java.util.Arrays; 28 29 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress; 30 import static junit.framework.TestCase.assertNull; 31 import static junit.framework.TestCase.fail; 32 import static org.mockito.BDDMockito.given; 33 import static org.mockito.Matchers.any; 34 import static org.mockito.Mockito.doThrow; 35 import static org.mockito.Mockito.mock; 36 37 @SuppressWarnings({"unchecked", "serial"}) 38 public class MockHandlerImplTest extends TestBase { 39 40 private StubbedInvocationMatcher stubbedInvocationMatcher = mock(StubbedInvocationMatcher.class); 41 private Invocation invocation = mock(InvocationImpl.class); 42 43 44 @Test should_remove_verification_mode_even_when_invalid_matchers()45 public void should_remove_verification_mode_even_when_invalid_matchers() throws Throwable { 46 // given 47 Invocation invocation = new InvocationBuilder().toInvocation(); 48 @SuppressWarnings("rawtypes") 49 MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl()); 50 mockingProgress().verificationStarted(VerificationModeFactory.atLeastOnce()); 51 handler.matchersBinder = new MatchersBinder() { 52 public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) { 53 throw new InvalidUseOfMatchersException(); 54 } 55 }; 56 57 try { 58 // when 59 handler.handle(invocation); 60 61 // then 62 fail(); 63 } catch (InvalidUseOfMatchersException ignored) { 64 } 65 66 assertNull(mockingProgress().pullVerificationMode()); 67 } 68 69 70 @Test(expected = MockitoException.class) should_throw_mockito_exception_when_invocation_handler_throws_anything()71 public void should_throw_mockito_exception_when_invocation_handler_throws_anything() throws Throwable { 72 // given 73 InvocationListener throwingListener = mock(InvocationListener.class); 74 doThrow(new Throwable()).when(throwingListener).reportInvocation(any(MethodInvocationReport.class)); 75 MockHandlerImpl<?> handler = create_correctly_stubbed_handler(throwingListener); 76 77 // when 78 handler.handle(invocation); 79 } 80 81 @Test(expected = WrongTypeOfReturnValue.class) should_report_bogus_default_answer()82 public void should_report_bogus_default_answer() throws Throwable { 83 MockSettingsImpl mockSettings = mock(MockSettingsImpl.class); 84 MockHandlerImpl<?> handler = new MockHandlerImpl(mockSettings); 85 given(mockSettings.getDefaultAnswer()).willReturn(new Returns(AWrongType.WRONG_TYPE)); 86 87 @SuppressWarnings("unused") // otherwise cast is not done 88 String there_should_not_be_a_CCE_here = (String) handler.handle( 89 new InvocationBuilder().method(Object.class.getDeclaredMethod("toString")).toInvocation() 90 ); 91 } 92 create_correctly_stubbed_handler(InvocationListener throwingListener)93 private MockHandlerImpl<?> create_correctly_stubbed_handler(InvocationListener throwingListener) { 94 MockHandlerImpl<?> handler = create_handler_with_listeners(throwingListener); 95 stub_ordinary_invocation_with_given_return_value(handler); 96 return handler; 97 } 98 stub_ordinary_invocation_with_given_return_value(MockHandlerImpl<?> handler)99 private void stub_ordinary_invocation_with_given_return_value(MockHandlerImpl<?> handler) { 100 stub_ordinary_invocation_with_invocation_matcher(handler, stubbedInvocationMatcher); 101 } 102 103 stub_ordinary_invocation_with_invocation_matcher(MockHandlerImpl<?> handler, StubbedInvocationMatcher value)104 private void stub_ordinary_invocation_with_invocation_matcher(MockHandlerImpl<?> handler, StubbedInvocationMatcher value) { 105 handler.invocationContainerImpl = mock(InvocationContainerImpl.class); 106 given(handler.invocationContainerImpl.findAnswerFor(any(InvocationImpl.class))).willReturn(value); 107 } 108 109 create_handler_with_listeners(InvocationListener... listener)110 private MockHandlerImpl<?> create_handler_with_listeners(InvocationListener... listener) { 111 @SuppressWarnings("rawtypes") 112 MockHandlerImpl<?> handler = new MockHandlerImpl(mock(MockSettingsImpl.class)); 113 handler.matchersBinder = mock(MatchersBinder.class); 114 given(handler.getMockSettings().getInvocationListeners()).willReturn(Arrays.asList(listener)); 115 return handler; 116 } 117 118 private static class AWrongType { 119 public static final AWrongType WRONG_TYPE = new AWrongType(); 120 } 121 } 122