/
MockSettingsImplTest.java
269 lines (224 loc) · 9.85 KB
/
MockSettingsImplTest.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.VerboseMockInvocationLogger;
import org.mockito.listeners.InvocationListener;
import org.mockito.listeners.StubbingLookupListener;
import org.mockitoutil.TestBase;
public class MockSettingsImplTest extends TestBase {
private MockSettingsImpl<?> mockSettingsImpl = new MockSettingsImpl<Object>();
@Mock private InvocationListener invocationListener;
@Mock private StubbingLookupListener stubbingLookupListener;
@Test
@SuppressWarnings("unchecked")
public void shouldNotAllowSettingNullInterface() {
assertThatThrownBy(
() -> {
mockSettingsImpl.extraInterfaces(List.class, null);
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("extraInterfaces() does not accept null parameters.");
}
@Test
@SuppressWarnings("unchecked")
public void shouldNotAllowNonInterfaces() {
assertThatThrownBy(
() -> {
mockSettingsImpl.extraInterfaces(List.class, LinkedList.class);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"extraInterfaces() accepts only interfaces",
"You passed following type: LinkedList which is not an interface.");
}
@Test
@SuppressWarnings("unchecked")
public void shouldNotAllowUsingTheSameInterfaceAsExtra() {
assertThatThrownBy(
() -> {
mockSettingsImpl.extraInterfaces(List.class, LinkedList.class);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"extraInterfaces() accepts only interfaces.",
"You passed following type: LinkedList which is not an interface.");
}
@Test
@SuppressWarnings("unchecked")
public void shouldNotAllowEmptyExtraInterfaces() {
assertThatThrownBy(
() -> {
mockSettingsImpl.extraInterfaces();
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("extraInterfaces() requires at least one interface.");
}
@Test
@SuppressWarnings("unchecked")
public void shouldNotAllowNullArrayOfExtraInterfaces() {
assertThatThrownBy(
() -> {
mockSettingsImpl.extraInterfaces((Class<?>[]) null);
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("extraInterfaces() requires at least one interface.");
}
@Test
@SuppressWarnings("unchecked")
public void shouldAllowMultipleInterfaces() {
// when
mockSettingsImpl.extraInterfaces(List.class, Set.class);
// then
assertThat(mockSettingsImpl.getExtraInterfaces().size()).isEqualTo(2);
assertThat(mockSettingsImpl.getExtraInterfaces()).contains(List.class);
assertThat(mockSettingsImpl.getExtraInterfaces()).contains(Set.class);
}
@Test
public void shouldSetMockToBeSerializable() {
// when
mockSettingsImpl.serializable();
// then
assertThat(mockSettingsImpl.isSerializable()).isTrue();
}
@Test
public void shouldKnowIfIsSerializable() {
// given
assertThat(mockSettingsImpl.isSerializable()).isFalse();
// when
mockSettingsImpl.serializable();
// then
assertThat(mockSettingsImpl.isSerializable()).isTrue();
}
@Test
public void shouldAddVerboseLoggingListener() {
// given
assertThat(mockSettingsImpl.hasInvocationListeners()).isFalse();
// when
mockSettingsImpl.verboseLogging();
// then
assertThat(mockSettingsImpl.getInvocationListeners())
.extracting("class")
.contains(VerboseMockInvocationLogger.class);
}
@Test
public void shouldAddVerboseLoggingListenerOnlyOnce() {
// given
assertThat(mockSettingsImpl.hasInvocationListeners()).isFalse();
// when
mockSettingsImpl.verboseLogging().verboseLogging();
// then
assertThat(mockSettingsImpl.getInvocationListeners()).hasSize(1);
}
@Test
@SuppressWarnings("unchecked")
public void shouldAddInvocationListener() {
// given
assertThat(mockSettingsImpl.hasInvocationListeners()).isFalse();
// when
mockSettingsImpl.invocationListeners(invocationListener);
// then
assertThat(mockSettingsImpl.getInvocationListeners()).contains(invocationListener);
}
@Test
@SuppressWarnings("unchecked")
public void canAddDuplicateInvocationListeners_ItsNotOurBusinessThere() {
// given
assertThat(mockSettingsImpl.hasInvocationListeners()).isFalse();
// when
mockSettingsImpl
.invocationListeners(invocationListener, invocationListener)
.invocationListeners(invocationListener);
// then
assertThat(mockSettingsImpl.getInvocationListeners())
.containsSequence(invocationListener, invocationListener, invocationListener);
}
@Test
public void validates_listeners() {
assertThatThrownBy(
() ->
mockSettingsImpl.addListeners(
new Object[] {}, new LinkedList<Object>(), "myListeners"))
.hasMessageContaining("myListeners() requires at least one listener");
assertThatThrownBy(
() ->
mockSettingsImpl.addListeners(
null, new LinkedList<Object>(), "myListeners"))
.hasMessageContaining("myListeners() does not accept null vararg array");
assertThatThrownBy(
() ->
mockSettingsImpl.addListeners(
new Object[] {null},
new LinkedList<Object>(),
"myListeners"))
.hasMessageContaining("myListeners() does not accept null listeners");
}
@Test
public void validates_stubbing_lookup_listeners() {
assertThatThrownBy(
() ->
mockSettingsImpl.stubbingLookupListeners(
new StubbingLookupListener[] {}))
.hasMessageContaining("stubbingLookupListeners() requires at least one listener");
assertThatThrownBy(() -> mockSettingsImpl.stubbingLookupListeners(null))
.hasMessageContaining(
"stubbingLookupListeners() does not accept null vararg array");
assertThatThrownBy(
() ->
mockSettingsImpl.stubbingLookupListeners(
new StubbingLookupListener[] {null}))
.hasMessageContaining("stubbingLookupListeners() does not accept null listeners");
}
@Test
public void validates_invocation_listeners() {
assertThatThrownBy(() -> mockSettingsImpl.invocationListeners(new InvocationListener[] {}))
.hasMessageContaining("invocationListeners() requires at least one listener");
assertThatThrownBy(() -> mockSettingsImpl.invocationListeners(null))
.hasMessageContaining("invocationListeners() does not accept null vararg array");
assertThatThrownBy(
() -> mockSettingsImpl.invocationListeners(new InvocationListener[] {null}))
.hasMessageContaining("invocationListeners() does not accept null listeners");
}
@Test
public void addListeners_has_empty_listeners_by_default() {
assertThat(mockSettingsImpl.getInvocationListeners()).isEmpty();
assertThat(mockSettingsImpl.getStubbingLookupListeners()).isEmpty();
}
@Test
public void addListeners_shouldAddMockObjectListeners() {
// when
mockSettingsImpl.invocationListeners(invocationListener);
mockSettingsImpl.stubbingLookupListeners(stubbingLookupListener);
// then
assertThat(mockSettingsImpl.getInvocationListeners()).contains(invocationListener);
assertThat(mockSettingsImpl.getStubbingLookupListeners()).contains(stubbingLookupListener);
}
@Test
public void addListeners_canAddDuplicateMockObjectListeners_ItsNotOurBusinessThere() {
// when
mockSettingsImpl
.stubbingLookupListeners(stubbingLookupListener)
.stubbingLookupListeners(stubbingLookupListener)
.invocationListeners(invocationListener)
.invocationListeners(invocationListener);
// then
assertThat(mockSettingsImpl.getInvocationListeners())
.containsSequence(invocationListener, invocationListener);
assertThat(mockSettingsImpl.getStubbingLookupListeners())
.containsSequence(stubbingLookupListener, stubbingLookupListener);
}
@Test
public void validates_strictness() {
assertThatThrownBy(() -> mockSettingsImpl.strictness(null))
.hasMessageContaining("strictness() does not accept null parameter");
}
}