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 static org.assertj.core.api.Assertions.assertThatThrownBy; 8 import static org.junit.Assert.assertNull; 9 import static org.junit.Assert.fail; 10 import static org.mockito.ArgumentMatchers.any; 11 import static org.mockito.BDDMockito.given; 12 import static org.mockito.Mockito.doThrow; 13 import static org.mockito.Mockito.mock; 14 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress; 15 16 import org.junit.Test; 17 import org.mockito.exceptions.base.MockitoException; 18 import org.mockito.exceptions.misusing.InvalidUseOfMatchersException; 19 import org.mockito.exceptions.misusing.WrongTypeOfReturnValue; 20 import org.mockito.internal.creation.MockSettingsImpl; 21 import org.mockito.internal.invocation.InvocationBuilder; 22 import org.mockito.internal.invocation.InvocationMatcher; 23 import org.mockito.internal.invocation.MatchersBinder; 24 import org.mockito.internal.progress.ArgumentMatcherStorage; 25 import org.mockito.internal.stubbing.answers.Returns; 26 import org.mockito.internal.verification.VerificationModeFactory; 27 import org.mockito.invocation.Invocation; 28 import org.mockito.listeners.InvocationListener; 29 import org.mockito.listeners.MethodInvocationReport; 30 import org.mockitoutil.TestBase; 31 32 @SuppressWarnings({"unchecked"}) 33 public class MockHandlerImplTest extends TestBase { 34 35 @Test should_remove_verification_mode_even_when_invalid_matchers()36 public void should_remove_verification_mode_even_when_invalid_matchers() throws Throwable { 37 // given 38 Invocation invocation = new InvocationBuilder().toInvocation(); 39 @SuppressWarnings("rawtypes") 40 MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl()); 41 mockingProgress().verificationStarted(VerificationModeFactory.atLeastOnce()); 42 handler.matchersBinder = 43 new MatchersBinder() { 44 public InvocationMatcher bindMatchers( 45 ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) { 46 throw new InvalidUseOfMatchersException(); 47 } 48 }; 49 50 try { 51 // when 52 handler.handle(invocation); 53 54 // then 55 fail(); 56 } catch (InvalidUseOfMatchersException ignored) { 57 } 58 59 assertNull(mockingProgress().pullVerificationMode()); 60 } 61 62 @Test should_throw_mockito_exception_when_invocation_handler_throws_anything()63 public void should_throw_mockito_exception_when_invocation_handler_throws_anything() { 64 // given 65 InvocationListener throwingListener = mock(InvocationListener.class); 66 // when / then 67 assertThatThrownBy( 68 () -> { 69 doThrow(new Throwable()) 70 .when(throwingListener) 71 .reportInvocation(any(MethodInvocationReport.class)); 72 }) 73 .isInstanceOf(MockitoException.class) 74 .hasMessageContainingAll( 75 "Checked exception is invalid for this method!", 76 "Invalid: java.lang.Throwable"); 77 } 78 79 @Test should_report_bogus_default_answer()80 public void should_report_bogus_default_answer() throws Throwable { 81 // given 82 MockSettingsImpl mockSettings = mock(MockSettingsImpl.class); 83 MockHandlerImpl<?> handler = new MockHandlerImpl(mockSettings); 84 given(mockSettings.getDefaultAnswer()).willReturn(new Returns(AWrongType.WRONG_TYPE)); 85 Invocation invocation = 86 new InvocationBuilder() 87 .method(Object.class.getDeclaredMethod("toString")) 88 .toInvocation(); 89 // when / then 90 assertThatThrownBy( 91 () -> { 92 @SuppressWarnings("unused") // otherwise cast is not done 93 String there_should_not_be_a_CCE_here = 94 (String) handler.handle(invocation); 95 }) 96 .isInstanceOf(WrongTypeOfReturnValue.class) 97 .hasMessageContainingAll( 98 "Default answer returned a result with the wrong type:", 99 "AWrongType cannot be returned by toString()", 100 "toString() should return String", 101 "The default answer of iMethods that was configured on the mock is probably incorrectly implemented."); 102 } 103 104 private static class AWrongType { 105 public static final AWrongType WRONG_TYPE = new AWrongType(); 106 } 107 } 108