1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.matchers; 6 7 import static org.junit.Assert.fail; 8 import static org.mockito.ArgumentMatchers.argThat; 9 import static org.mockito.Mockito.verify; 10 11 import org.junit.Test; 12 import org.mockito.ArgumentMatcher; 13 import org.mockito.Mock; 14 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent; 15 import org.mockitousage.IMethods; 16 import org.mockitoutil.TestBase; 17 18 public class CustomMatcherDoesYieldCCETest extends TestBase { 19 20 @Mock private IMethods mock; 21 22 @Test shouldNotThrowCCE()23 public void shouldNotThrowCCE() { 24 mock.simpleMethod(new Object()); 25 26 try { 27 // calling overloaded method so that matcher will be called with 28 // different type 29 verify(mock).simpleMethod(argThat(isStringWithTextFoo())); 30 fail(); 31 } catch (ArgumentsAreDifferent e) { 32 } 33 } 34 isStringWithTextFoo()35 private ArgumentMatcher<String> isStringWithTextFoo() { 36 return new ArgumentMatcher<String>() { 37 public boolean matches(String argument) { 38 // casting that should not be thrown: 39 String str = (String) argument; 40 return str.equals("foo"); 41 } 42 }; 43 } 44 } 45