• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.exceptions.base.MockitoAssertionError;
11 import org.mockitoutil.TestBase;
12 
13 import java.util.List;
14 
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.*;
17 
18 public class AtLeastXVerificationTest extends TestBase {
19 
20     @Mock private List<String> mock;
21 
22     @Test
shouldVerifyAtLeastXTimes()23     public void shouldVerifyAtLeastXTimes() throws Exception {
24         //when
25         mock.clear();
26         mock.clear();
27         mock.clear();
28 
29         //then
30         verify(mock, atLeast(2)).clear();
31     }
32 
33     @Test
shouldFailVerificationAtLeastXTimes()34     public void shouldFailVerificationAtLeastXTimes() throws Exception {
35         mock.add("one");
36         verify(mock, atLeast(1)).add(anyString());
37 
38         try {
39             verify(mock, atLeast(2)).add(anyString());
40             fail();
41         } catch (MockitoAssertionError e) {}
42     }
43 
44     @Test
shouldAllowAtLeastZeroForTheSakeOfVerifyNoMoreInteractionsSometimes()45     public void shouldAllowAtLeastZeroForTheSakeOfVerifyNoMoreInteractionsSometimes() throws Exception {
46         //when
47         mock.add("one");
48         mock.clear();
49 
50         //then
51         verify(mock, atLeast(0)).add("one");
52         verify(mock, atLeast(0)).clear();
53 
54         verifyNoMoreInteractions(mock);
55     }
56 }
57