forked from mockito/mockito
/
SpyingOnRealObjectsTest.java
205 lines (168 loc) · 5.2 KB
/
SpyingOnRealObjectsTest.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.spies;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.verification.NoInteractionsWanted;
import org.mockito.exceptions.verification.TooFewActualInvocations;
import org.mockito.exceptions.verification.VerificationInOrderFailure;
import org.mockitoutil.TestBase;
public class SpyingOnRealObjectsTest extends TestBase {
List<String> list = new LinkedList<String>();
List<String> spy = Mockito.spy(list);
@Test
public void shouldVerify() {
spy.add("one");
spy.add("two");
assertEquals("one", spy.get(0));
assertEquals("two", spy.get(1));
verify(spy).add("one");
verify(spy).add("two");
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test
public void shouldBeAbleToMockObjectBecauseWhyNot() {
spy(new Object());
}
@Test
public void shouldStub() {
spy.add("one");
when(spy.get(0)).thenReturn("1").thenReturn("1 again");
assertEquals("1", spy.get(0));
assertEquals("1 again", spy.get(0));
assertEquals("one", spy.iterator().next());
assertEquals(1, spy.size());
}
@Test
public void shouldAllowOverridingStubs() {
when(spy.contains(anyObject())).thenReturn(true);
when(spy.contains("foo")).thenReturn(false);
assertTrue(spy.contains("bar"));
assertFalse(spy.contains("foo"));
}
@Test
public void shouldStubVoid() {
doNothing().doThrow(new RuntimeException()).when(spy).clear();
spy.add("one");
spy.clear();
try {
spy.clear();
fail();
} catch (RuntimeException e) {
}
assertEquals(1, spy.size());
}
@Test
public void shouldStubWithDoReturnAndVerify() {
doReturn("foo").doReturn("bar").when(spy).get(0);
assertEquals("foo", spy.get(0));
assertEquals("bar", spy.get(0));
verify(spy, times(2)).get(0);
verifyNoMoreInteractions(spy);
}
@Test
public void shouldVerifyInOrder() {
spy.add("one");
spy.add("two");
InOrder inOrder = inOrder(spy);
inOrder.verify(spy).add("one");
inOrder.verify(spy).add("two");
verifyNoMoreInteractions(spy);
}
@Test
public void shouldVerifyInOrderAndFail() {
spy.add("one");
spy.add("two");
InOrder inOrder = inOrder(spy);
inOrder.verify(spy).add("two");
try {
inOrder.verify(spy).add("one");
fail();
} catch (VerificationInOrderFailure f) {
}
}
@Test
public void shouldVerifyNumberOfTimes() {
spy.add("one");
spy.add("one");
verify(spy, times(2)).add("one");
verifyNoMoreInteractions(spy);
}
@Test
public void shouldVerifyNumberOfTimesAndFail() {
spy.add("one");
spy.add("one");
try {
verify(spy, times(3)).add("one");
fail();
} catch (TooFewActualInvocations e) {
}
}
@Test
public void shouldVerifyNoMoreInteractionsAndFail() {
spy.add("one");
spy.add("two");
verify(spy).add("one");
try {
verifyNoMoreInteractions(spy);
fail();
} catch (NoInteractionsWanted e) {
}
}
@Test
public void shouldToString() {
spy.add("foo");
assertEquals("[foo]", spy.toString());
}
interface Foo {
String print();
}
@Test
public void shouldAllowSpyingAnonymousClasses() {
// when
Foo spy =
spy(
new Foo() {
public String print() {
return "foo";
}
});
// then
assertEquals("foo", spy.print());
}
@Test
public void shouldSayNiceMessageWhenSpyingOnPrivateClass() throws Exception {
List<String> real = Arrays.asList("first", "second");
try {
List<String> spy = spy(real);
assumeTrue(
"Using inline mocks, it is possible to spy on private types",
spy.getClass() != real.getClass());
fail();
} catch (MockitoException e) {
assertThat(e)
.hasMessageContaining(
"Most likely it is due to mocking a private class that is not visible to Mockito");
}
}
@Test
public void spysHashCodeEqualsDelegatedToActualMethods() {
List<String> real = new ArrayList<>();
real.add("one");
List<String> spy = spy(real);
assertEquals(real.hashCode(), spy.hashCode());
assertTrue(spy.equals(real));
}
}