forked from mockito/mockito
/
NumberOfInvocationsCheckerTest.java
186 lines (145 loc) · 6.73 KB
/
NumberOfInvocationsCheckerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification.checkers;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.exceptions.verification.NeverWantedButInvoked;
import org.mockito.exceptions.verification.TooFewActualInvocations;
import org.mockito.exceptions.verification.TooManyActualInvocations;
import org.mockito.internal.invocation.InvocationBuilder;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.invocation.Invocation;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockitousage.IMethods;
@RunWith(MockitoJUnitRunner.class)
public class NumberOfInvocationsCheckerTest {
private InvocationMatcher wanted;
private List<Invocation> invocations;
@Mock private IMethods mock;
@Rule public ExpectedException exception = ExpectedException.none();
@Rule public TestName testName = new TestName();
@Test
public void shouldReportTooFewActual() throws Exception {
wanted = buildSimpleMethod().toInvocationMatcher();
invocations =
asList(buildSimpleMethod().toInvocation(), buildSimpleMethod().toInvocation());
exception.expect(TooFewActualInvocations.class);
exception.expectMessage("mock.simpleMethod()");
exception.expectMessage("Wanted 100 times");
exception.expectMessage("But was 2 times");
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);
}
@Test
public void shouldReportAllInvocationsStackTrace() throws Exception {
wanted = buildSimpleMethod().toInvocationMatcher();
invocations =
asList(buildSimpleMethod().toInvocation(), buildSimpleMethod().toInvocation());
exception.expect(TooFewActualInvocations.class);
exception.expectMessage("mock.simpleMethod()");
exception.expectMessage("Wanted 100 times");
exception.expectMessage("But was 2 times");
exception.expectMessage(containsTimes("-> at", 3));
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);
}
@Test
public void shouldNotReportWithLastInvocationStackTraceIfNoInvocationsFound() throws Exception {
invocations = emptyList();
wanted = buildSimpleMethod().toInvocationMatcher();
exception.expect(TooFewActualInvocations.class);
exception.expectMessage("mock.simpleMethod()");
exception.expectMessage("Wanted 100 times");
exception.expectMessage("But was 0 times");
exception.expectMessage(containsTimes("-> at", 1));
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);
}
@Test
public void shouldReportWithAllInvocationsStackTrace() throws Exception {
Invocation first = buildSimpleMethod().toInvocation();
Invocation second = buildSimpleMethod().toInvocation();
Invocation third = buildSimpleMethod().toInvocation();
invocations = asList(first, second, third);
wanted = buildSimpleMethod().toInvocationMatcher();
exception.expect(TooManyActualInvocations.class);
exception.expectMessage("" + first.getLocation());
exception.expectMessage("" + second.getLocation());
exception.expectMessage("" + third.getLocation());
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 2);
}
@Test
public void shouldReportTooManyActual() throws Exception {
Invocation first = buildSimpleMethod().toInvocation();
Invocation second = buildSimpleMethod().toInvocation();
invocations = asList(first, second);
wanted = buildSimpleMethod().toInvocationMatcher();
exception.expectMessage("Wanted 1 time");
exception.expectMessage("But was 2 times");
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 1);
}
@Test
public void shouldReportNeverWantedButInvokedWithArgs() throws Exception {
Invocation invocation = buildSimpleMethodWithArgs("arg1").toInvocation();
invocations = Collections.singletonList(invocation);
wanted = buildSimpleMethodWithArgs("arg1").toInvocationMatcher();
exception.expect(NeverWantedButInvoked.class);
exception.expectMessage("Never wanted here");
exception.expectMessage("But invoked here");
exception.expectMessage("" + invocation.getLocation());
exception.expectMessage("with next arguments: [arg1]");
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 0);
}
@Test
public void shouldMarkInvocationsAsVerified() throws Exception {
Invocation invocation = buildSimpleMethod().toInvocation();
assertThat(invocation.isVerified()).isFalse();
invocations = asList(invocation);
wanted = buildSimpleMethod().toInvocationMatcher();
NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 1);
assertThat(invocation.isVerified()).isTrue();
}
private InvocationBuilder buildSimpleMethod() {
return new InvocationBuilder().mock(mock).simpleMethod();
}
private InvocationBuilder buildSimpleMethodWithArgs(String arg) {
return new InvocationBuilder().mock(mock).simpleMethod().args(arg);
}
private static BaseMatcher<String> containsTimes(String value, int amount) {
return new StringContainsNumberMatcher(value, amount);
}
private static class StringContainsNumberMatcher extends TypeSafeMatcher<String> {
private final String expected;
private final int amount;
StringContainsNumberMatcher(String expected, int amount) {
this.expected = expected;
this.amount = amount;
}
public boolean matchesSafely(String text) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = text.indexOf(expected, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += expected.length();
}
}
return count == amount;
}
public void describeTo(Description description) {
description.appendText("containing '" + expected + "' exactly " + amount + " times");
}
}
}