1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.bugs; 6 7 import static org.assertj.core.api.Assertions.assertThatThrownBy; 8 import static org.mockito.Mockito.mock; 9 import static org.mockito.Mockito.verifyNoInteractions; 10 11 import org.junit.Test; 12 import org.mockito.exceptions.misusing.WrongTypeOfReturnValue; 13 import org.mockito.exceptions.verification.NoInteractionsWanted; 14 import org.mockito.stubbing.Answer; 15 16 public class ClassCastExOnVerifyZeroInteractionsTest { 17 public interface TestMock { m1()18 boolean m1(); 19 } 20 21 @Test should_not_throw_ClassCastException_when_mock_verification_fails()22 public void should_not_throw_ClassCastException_when_mock_verification_fails() { 23 TestMock test = mock(TestMock.class, (Answer<Object>) invocation -> false); 24 test.m1(); 25 26 assertThatThrownBy( 27 () -> { 28 verifyNoInteractions(test); 29 }) 30 .isInstanceOf(NoInteractionsWanted.class) 31 .hasMessageContainingAll( 32 "No interactions wanted here:", 33 "But found these interactions on mock 'testMock':", 34 "Actually, above is the only interaction with this mock."); 35 } 36 37 @Test should_report_bogus_default_answer()38 public void should_report_bogus_default_answer() { 39 TestMock test = mock(TestMock.class, (Answer<Object>) invocation -> false); 40 41 assertThatThrownBy( 42 () -> { 43 String ignored = test.toString(); 44 }) 45 .isInstanceOf(WrongTypeOfReturnValue.class) 46 .hasMessageContainingAll( 47 "Default answer returned a result with the wrong type:", 48 "Boolean cannot be returned by toString()", 49 "toString() should return String", 50 "The default answer of testMock that was configured on the mock is probably incorrectly implemented."); 51 } 52 } 53