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 org.junit.Test; 8 import org.mockito.ArgumentMatcher; 9 import org.mockito.Mock; 10 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 11 import org.mockitousage.IMethods; 12 import org.mockitoutil.TestBase; 13 14 import static org.junit.Assert.fail; 15 import static org.mockito.Matchers.argThat; 16 import static org.mockito.Mockito.verify; 17 18 public class CustomMatcherDoesYieldCCETest extends TestBase { 19 20 @Mock 21 private IMethods mock; 22 23 @Test shouldNotThrowCCE()24 public void shouldNotThrowCCE() { 25 mock.simpleMethod(new Object()); 26 27 try { 28 // calling overloaded method so that matcher will be called with 29 // different type 30 verify(mock).simpleMethod(argThat(isStringWithTextFoo())); 31 fail(); 32 } catch (ArgumentsAreDifferent e) { 33 } 34 } 35 isStringWithTextFoo()36 private ArgumentMatcher<String> isStringWithTextFoo() { 37 return new ArgumentMatcher<String>() { 38 public boolean matches(String argument) { 39 // casting that should not be thrown: 40 String str = (String) argument; 41 return str.equals("foo"); 42 } 43 }; 44 } 45 } 46