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 static org.assertj.core.api.Assertions.assertThat; 8 import static org.junit.Assert.fail; 9 import static org.mockito.Mockito.times; 10 11 import java.util.LinkedList; 12 13 import org.junit.Test; 14 import org.mockito.Mock; 15 import org.mockito.Mockito; 16 import org.mockito.exceptions.verification.TooFewActualInvocations; 17 import org.mockito.exceptions.verification.TooManyActualInvocations; 18 import org.mockitoutil.TestBase; 19 20 @SuppressWarnings("unchecked") 21 public class DescriptiveMessagesWhenTimesXVerificationFailsTest extends TestBase { 22 23 @Mock private LinkedList mock; 24 25 @Test shouldVerifyActualNumberOfInvocationsSmallerThanWanted()26 public void shouldVerifyActualNumberOfInvocationsSmallerThanWanted() throws Exception { 27 mock.clear(); 28 mock.clear(); 29 mock.clear(); 30 31 Mockito.verify(mock, times(3)).clear(); 32 try { 33 Mockito.verify(mock, times(100)).clear(); 34 fail(); 35 } catch (TooFewActualInvocations e) { 36 assertThat(e) 37 .hasMessageContaining("mock.clear();") 38 .hasMessageContaining("Wanted 100 times") 39 .hasMessageContaining("was 3"); 40 } 41 } 42 43 @Test shouldVerifyActualNumberOfInvocationsLargerThanWanted()44 public void shouldVerifyActualNumberOfInvocationsLargerThanWanted() throws Exception { 45 mock.clear(); 46 mock.clear(); 47 mock.clear(); 48 mock.clear(); 49 50 Mockito.verify(mock, times(4)).clear(); 51 try { 52 Mockito.verify(mock, times(1)).clear(); 53 fail(); 54 } catch (TooManyActualInvocations e) { 55 assertThat(e) 56 .hasMessageContaining("mock.clear();") 57 .hasMessageContaining("Wanted 1 time") 58 .hasMessageContaining("was 4"); 59 } 60 } 61 } 62