1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitousage.verification; 7 8 import org.junit.Test; 9 import org.mockito.Mock; 10 import org.mockito.Mockito; 11 import org.mockito.exceptions.verification.TooLittleActualInvocations; 12 import org.mockito.exceptions.verification.TooManyActualInvocations; 13 import org.mockitoutil.TestBase; 14 15 import java.util.LinkedList; 16 17 import static junit.framework.TestCase.fail; 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.mockito.Mockito.times; 20 21 @SuppressWarnings("unchecked") 22 public class DescriptiveMessagesWhenTimesXVerificationFailsTest extends TestBase { 23 24 @Mock private LinkedList mock; 25 26 @Test shouldVerifyActualNumberOfInvocationsSmallerThanWanted()27 public void shouldVerifyActualNumberOfInvocationsSmallerThanWanted() throws Exception { 28 mock.clear(); 29 mock.clear(); 30 mock.clear(); 31 32 Mockito.verify(mock, times(3)).clear(); 33 try { 34 Mockito.verify(mock, times(100)).clear(); 35 fail(); 36 } catch (TooLittleActualInvocations e) { 37 assertThat(e) 38 .hasMessageContaining("mock.clear();") 39 .hasMessageContaining("Wanted 100 times") 40 .hasMessageContaining("was 3"); 41 } 42 } 43 44 @Test shouldVerifyActualNumberOfInvocationsLargerThanWanted()45 public void shouldVerifyActualNumberOfInvocationsLargerThanWanted() throws Exception { 46 mock.clear(); 47 mock.clear(); 48 mock.clear(); 49 mock.clear(); 50 51 Mockito.verify(mock, times(4)).clear(); 52 try { 53 Mockito.verify(mock, times(1)).clear(); 54 fail(); 55 } catch (TooManyActualInvocations e) { 56 assertThat(e) 57 .hasMessageContaining("mock.clear();") 58 .hasMessageContaining("Wanted 1 time") 59 .hasMessageContaining("was 4"); 60 } 61 } 62 } 63