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