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.assertEquals; 9 import static org.junit.Assert.fail; 10 import static org.mockito.Mockito.anyString; 11 import static org.mockito.Mockito.atMost; 12 import static org.mockito.Mockito.atMostOnce; 13 import static org.mockito.Mockito.inOrder; 14 import static org.mockito.Mockito.verify; 15 import static org.mockito.Mockito.verifyNoMoreInteractions; 16 17 import java.util.List; 18 19 import org.junit.Test; 20 import org.mockito.InOrder; 21 import org.mockito.Mock; 22 import org.mockito.exceptions.base.MockitoException; 23 import org.mockito.exceptions.verification.MoreThanAllowedActualInvocations; 24 import org.mockito.exceptions.verification.NoInteractionsWanted; 25 import org.mockito.verification.VerificationMode; 26 import org.mockitoutil.TestBase; 27 28 public class AtMostXVerificationTest extends TestBase { 29 30 @Mock private List<String> mock; 31 32 @Test shouldVerifyAtMostXTimes()33 public void shouldVerifyAtMostXTimes() throws Exception { 34 mock.clear(); 35 mock.clear(); 36 37 verify(mock, atMost(2)).clear(); 38 verify(mock, atMost(3)).clear(); 39 40 try { 41 verify(mock, atMostOnce()).clear(); 42 fail(); 43 } catch (MoreThanAllowedActualInvocations e) { 44 } 45 } 46 47 @Test shouldWorkWithArgumentMatchers()48 public void shouldWorkWithArgumentMatchers() throws Exception { 49 mock.add("one"); 50 verify(mock, atMost(5)).add(anyString()); 51 52 try { 53 verify(mock, atMost(0)).add(anyString()); 54 fail(); 55 } catch (MoreThanAllowedActualInvocations e) { 56 } 57 } 58 59 @Test shouldNotAllowNegativeNumber()60 public void shouldNotAllowNegativeNumber() throws Exception { 61 try { 62 verify(mock, atMost(-1)).clear(); 63 fail(); 64 } catch (MockitoException e) { 65 assertEquals("Negative value is not allowed here", e.getMessage()); 66 } 67 } 68 69 @Test shouldPrintDecentMessage()70 public void shouldPrintDecentMessage() throws Exception { 71 mock.clear(); 72 mock.clear(); 73 74 try { 75 verify(mock, atMostOnce()).clear(); 76 fail(); 77 } catch (MoreThanAllowedActualInvocations e) { 78 assertEquals("\nWanted at most 1 time but was 2", e.getMessage()); 79 } 80 } 81 82 @Test shouldNotAllowInOrderMode()83 public void shouldNotAllowInOrderMode() throws Exception { 84 mock.clear(); 85 InOrder inOrder = inOrder(mock); 86 87 try { 88 inOrder.verify(mock, atMostOnce()).clear(); 89 fail(); 90 } catch (MockitoException e) { 91 assertEquals("AtMost is not implemented to work with InOrder", e.getMessage()); 92 } 93 } 94 95 @Test shouldMarkInteractionsAsVerified()96 public void shouldMarkInteractionsAsVerified() throws Exception { 97 mock.clear(); 98 mock.clear(); 99 100 verify(mock, atMost(3)).clear(); 101 verifyNoMoreInteractions(mock); 102 } 103 104 @Test shouldDetectUnverifiedInMarkInteractionsAsVerified()105 public void shouldDetectUnverifiedInMarkInteractionsAsVerified() throws Exception { 106 mock.clear(); 107 mock.clear(); 108 undesiredInteraction(); 109 110 verify(mock, atMost(3)).clear(); 111 try { 112 verifyNoMoreInteractions(mock); 113 fail(); 114 } catch (NoInteractionsWanted e) { 115 assertThat(e).hasMessageContaining("undesiredInteraction("); 116 } 117 } 118 119 @Test should_return_formatted_output_from_toString_method()120 public void should_return_formatted_output_from_toString_method() { 121 VerificationMode atMost = atMost(3); 122 123 assertThat(atMost).hasToString("Wanted invocations count: at most 3"); 124 } 125 undesiredInteraction()126 private void undesiredInteraction() { 127 mock.add(""); 128 } 129 } 130