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