forked from mockito/mockito-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchersTest.kt
345 lines (299 loc) · 8.17 KB
/
MatchersTest.kt
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package test
import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import org.junit.Test
import org.mockito.ArgumentMatcher
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.*
import org.mockito.stubbing.Answer
import java.io.IOException
import kotlin.reflect.KClass
class MatchersTest : TestBase() {
@Test
fun anyString() {
mock<Methods>().apply {
string("")
verify(this).string(any())
}
}
@Test
fun anyInt() {
mock<Methods>().apply {
int(3)
verify(this).int(any())
}
}
@Test
fun anyClosedClass() {
mock<Methods>().apply {
closed(Closed())
verify(this).closed(any())
}
}
@Test
fun anyIntArray() {
mock<Methods>().apply {
intArray(intArrayOf())
verify(this).intArray(any())
}
}
@Test
fun anyClassArray() {
mock<Methods>().apply {
closedArray(arrayOf(Closed()))
verify(this).closedArray(anyArray())
}
}
@Test
fun anyNullableClassArray() {
mock<Methods>().apply {
closedNullableArray(arrayOf(Closed(), null))
verify(this).closedNullableArray(anyArray())
}
}
@Test
fun anyStringVararg() {
mock<Methods>().apply {
closedVararg(Closed(), Closed())
verify(this).closedVararg(anyVararg())
}
}
@Test
fun anyVarargMatching() {
mock<Methods>().apply {
whenever(varargBooleanResult(anyVararg())).thenReturn(true)
expect(varargBooleanResult()).toBe(true)
}
}
@Test
fun anyNull_neverVerifiesAny() {
mock<Methods>().apply {
nullableString(null)
verify(this, never()).nullableString(any())
}
}
@Test
fun anyNull_verifiesAnyOrNull() {
mock<Methods>().apply {
nullableString(null)
verify(this).nullableString(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveBoolean() {
mock<Methods>().apply {
boolean(false)
verify(this).boolean(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveByte() {
mock<Methods>().apply {
byte(3)
verify(this).byte(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveChar() {
mock<Methods>().apply {
char('a')
verify(this).char(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveShort() {
mock<Methods>().apply {
short(3)
verify(this).short(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveInt() {
mock<Methods>().apply {
int(3)
verify(this).int(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveLong() {
mock<Methods>().apply {
long(3)
verify(this).long(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveFloat() {
mock<Methods>().apply {
float(3f)
verify(this).float(anyOrNull())
}
}
@Test
fun anyNull_forPrimitiveDouble() {
mock<Methods>().apply {
double(3.0)
verify(this).double(anyOrNull())
}
}
/** https://github.com/nhaarman/mockito-kotlin/issues/27 */
@Test
fun anyThrowableWithSingleThrowableConstructor() {
mock<Methods>().apply {
throwableClass(ThrowableClass(IOException()))
verify(this).throwableClass(any())
}
}
@Test
fun listArgThat() {
mock<Methods>().apply {
closedList(listOf(Closed(), Closed()))
verify(this).closedList(
argThat {
size == 2
}
)
}
}
@Test
fun listArgForWhich() {
mock<Methods>().apply {
closedList(listOf(Closed(), Closed()))
verify(this).closedList(
argForWhich {
size == 2
}
)
}
}
@Test
fun listArgWhere() {
mock<Methods>().apply {
closedList(listOf(Closed(), Closed()))
verify(this).closedList(
argWhere {
it.size == 2
}
)
}
}
@Test
fun listArgCheck() {
mock<Methods>().apply {
closedList(listOf(Closed(), Closed()))
verify(this).closedList(
check {
expect(it.size).toBe(2)
}
)
}
}
@Test
fun checkProperlyFails() {
mock<Methods>().apply {
closedList(listOf(Closed(), Closed()))
expectErrorWithMessage("Argument(s) are different!") on {
verify(this).closedList(
check {
expect(it.size).toBe(1)
}
)
}
}
}
@Test
fun checkWithNullArgument_throwsError() {
mock<Methods>().apply {
nullableString(null)
expectErrorWithMessage("null").on {
verify(this).nullableString(check {})
}
}
}
@Test
fun isA_withNonNullableString() {
mock<Methods>().apply {
string("")
verify(this).string(isA<String>())
}
}
@Test
fun isA_withNullableString() {
mock<Methods>().apply {
nullableString("")
verify(this).nullableString(isA<String>())
}
}
@Test
fun same_withNonNullArgument() {
mock<Methods>().apply {
string("")
verify(this).string(same(""))
}
}
@Test
fun same_withNullableNonNullArgument() {
mock<Methods>().apply {
nullableString("")
verify(this).nullableString(same(""))
}
}
@Test
fun same_withNullArgument() {
mock<Methods>().apply {
nullableString(null)
verify(this).nullableString(same(null))
}
}
@Test
fun testVarargAnySuccess() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "b"
val matcher = VarargAnyMatcher({ "b" == it }, String::class.java, true, false)
/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
/* Then */
expect(t.varargBooleanResult("a", "b", "c")).toBe(true)
}
@Test
fun testVarargAnyFail() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "d"
val matcher = VarargAnyMatcher({ "d" == it }, String::class.java, true, false)
/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
/* Then */
expect(t.varargBooleanResult("a", "b", "c")).toBe(false)
}
/** https://github.com/nhaarman/mockito-kotlin/issues/328 */
@Test
fun testRefEqForNonNullableParameter() {
mock<Methods>().apply {
/* When */
val array = intArrayOf(2, 3)
intArray(array)
/* Then */
verify(this).intArray(refEq(array))
}
}
/**
* a VarargMatcher implementation for varargs of type [T] that will answer with type [R] if any of the var args
* matched. Needs to keep state between matching invocations.
*/
private class VarargAnyMatcher<T, R>(
private val match: ((T) -> Boolean),
private val clazz: Class<T>,
private val success: R,
private val failure: R
) : ArgumentMatcher<T>, Answer<R> {
private var anyMatched = false
override fun matches(t: T): Boolean {
@Suppress("UNCHECKED_CAST") // No idea how to solve this better
anyMatched = (t as Array<T>).any(match)
return anyMatched
}
override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
}
}