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.verification; 6 7 import org.junit.Test; 8 import org.mockito.Mock; 9 import org.mockito.exceptions.base.MockitoAssertionError; 10 import org.mockito.internal.invocation.InvocationMatcher; 11 import org.mockito.internal.verification.api.VerificationData; 12 import org.mockito.invocation.Invocation; 13 import org.mockito.verification.VerificationMode; 14 import org.mockitousage.IMethods; 15 import org.mockitoutil.TestBase; 16 17 import static junit.framework.TestCase.fail; 18 import static org.mockito.Mockito.verify; 19 20 public class CustomVerificationTest extends TestBase { 21 22 @Mock IMethods mock; 23 24 @Test custom_verification_with_old_api()25 public void custom_verification_with_old_api() { 26 //given: 27 mock.simpleMethod("a", 10); 28 29 //expect: 30 verify(mock, ignoreParametersUsingOldApi()).simpleMethod(); 31 32 try { 33 verify(mock, ignoreParametersUsingOldApi()).otherMethod(); 34 fail(); 35 } catch (MockitoAssertionError e) {} 36 } 37 38 //Old api still supported, see https://github.com/mockito/mockito/issues/730 ignoreParametersUsingOldApi()39 private VerificationMode ignoreParametersUsingOldApi() { 40 return new VerificationMode() { 41 public void verify(VerificationData data) { 42 //use old api 43 InvocationMatcher target = data.getWanted(); 44 45 //sanity check the new api 46 if (data.getTarget() != target) { 47 throw new RuntimeException("Sanity check"); 48 } 49 50 //look for the relevant invocation and exit if found 51 for (Invocation invocation : data.getAllInvocations()) { 52 if (target.getInvocation().getMethod().getName().equals(invocation.getMethod().getName())) { 53 return; 54 } 55 } 56 57 //verification failed! 58 throw new MockitoAssertionError("Expected method with name: " + target + " not found in:\n" + data.getAllInvocations()); 59 } 60 public VerificationMode description(String description) { 61 return this; 62 } 63 }; 64 } 65 } 66