1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockitousage.verification;
7
8import org.junit.Before;
9import org.junit.Test;
10import org.mockito.InOrder;
11import org.mockito.exceptions.verification.*;
12import org.mockitoutil.TestBase;
13
14import java.util.LinkedList;
15
16import static org.junit.Assert.fail;
17import static org.assertj.core.api.Assertions.assertThat;
18import static org.mockito.Mockito.*;
19
20@SuppressWarnings("unchecked")
21public class ExactNumberOfTimesVerificationTest extends TestBase {
22
23    private LinkedList<String> mock;
24
25    @Before
26    public void setup() {
27        mock = mock(LinkedList.class);
28    }
29
30    @Test
31    public void shouldDetectTooLittleActualInvocations() throws Exception {
32        mock.clear();
33        mock.clear();
34
35        verify(mock, times(2)).clear();
36        try {
37            verify(mock, times(100)).clear();
38            fail();
39        } catch (TooLittleActualInvocations e) {
40            assertThat(e)
41                .hasMessageContaining("Wanted 100 times")
42                .hasMessageContaining("was 2");
43        }
44    }
45
46    @Test
47    public void shouldDetectTooManyActualInvocations() throws Exception {
48        mock.clear();
49        mock.clear();
50
51        verify(mock, times(2)).clear();
52        try {
53            verify(mock, times(1)).clear();
54            fail();
55        } catch (TooManyActualInvocations e) {
56            assertThat(e)
57                .hasMessageContaining("Wanted 1 time")
58                .hasMessageContaining("was 2 times");
59        }
60    }
61
62    @Test
63    public void shouldDetectActualInvocationsCountIsMoreThanZero() throws Exception {
64        verify(mock, times(0)).clear();
65        try {
66            verify(mock, times(15)).clear();
67            fail();
68        } catch (WantedButNotInvoked e) {}
69    }
70
71    @Test
72    public void shouldDetectActuallyCalledOnce() throws Exception {
73        mock.clear();
74
75        try {
76            verify(mock, times(0)).clear();
77            fail();
78        } catch (NeverWantedButInvoked e) {
79            assertThat(e).hasMessageContaining("Never wanted here");
80        }
81    }
82
83    @Test
84    public void shouldPassWhenMethodsActuallyNotCalled() throws Exception {
85        verify(mock, times(0)).clear();
86        verify(mock, times(0)).add("yes, I wasn't called");
87    }
88
89    @Test
90    public void shouldNotCountInStubbedInvocations() throws Exception {
91        when(mock.add("test")).thenReturn(false);
92        when(mock.add("test")).thenReturn(true);
93
94        mock.add("test");
95        mock.add("test");
96
97        verify(mock, times(2)).add("test");
98    }
99
100    @Test
101    public void shouldAllowVerifyingInteractionNeverHappened() throws Exception {
102        mock.add("one");
103
104        verify(mock, never()).add("two");
105        verify(mock, never()).clear();
106
107        try {
108            verify(mock, never()).add("one");
109            fail();
110        } catch (NeverWantedButInvoked e) {}
111    }
112
113    @Test
114    public void shouldAllowVerifyingInteractionNeverHappenedInOrder() throws Exception {
115        mock.add("one");
116        mock.add("two");
117
118        InOrder inOrder = inOrder(mock);
119
120        inOrder.verify(mock, never()).add("xxx");
121        inOrder.verify(mock).add("one");
122        inOrder.verify(mock, never()).add("one");
123
124        try {
125            inOrder.verify(mock, never()).add("two");
126            fail();
127        } catch (VerificationInOrderFailure e) {}
128    }
129}
130