• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test
2 
3 import com.nhaarman.expect.expect
4 import com.nhaarman.expect.expectErrorWithMessage
5 import org.junit.Test
6 import org.mockito.ArgumentMatcher
7 import org.mockito.internal.matchers.VarargMatcher
8 import org.mockito.invocation.InvocationOnMock
9 import org.mockito.kotlin.*
10 import org.mockito.stubbing.Answer
11 import java.io.IOException
12 import kotlin.check
13 
14 class MatchersTest : TestBase() {
15 
16     @Test
anyStringnull17     fun anyString() {
18         mock<Methods>().apply {
19             string("")
20             verify(this).string(any())
21         }
22     }
23 
24     @Test
anyIntnull25     fun anyInt() {
26         mock<Methods>().apply {
27             int(3)
28             verify(this).int(any())
29         }
30     }
31 
32     @Test
anyClosedClassnull33     fun anyClosedClass() {
34         mock<Methods>().apply {
35             closed(Closed())
36             verify(this).closed(any())
37         }
38     }
39 
40     @Test
anyIntArraynull41     fun anyIntArray() {
42         mock<Methods>().apply {
43             intArray(intArrayOf())
44             verify(this).intArray(any())
45         }
46     }
47 
48     @Test
anyClassArraynull49     fun anyClassArray() {
50         mock<Methods>().apply {
51             closedArray(arrayOf(Closed()))
52             verify(this).closedArray(anyArray())
53         }
54     }
55 
56     @Test
anyNullableClassArraynull57     fun anyNullableClassArray() {
58         mock<Methods>().apply {
59             closedNullableArray(arrayOf(Closed(), null))
60             verify(this).closedNullableArray(anyArray())
61         }
62     }
63 
64     @Test
anyStringVarargnull65     fun anyStringVararg() {
66         mock<Methods>().apply {
67             closedVararg(Closed(), Closed())
68             verify(this).closedVararg(anyVararg())
69         }
70     }
71 
72     @Test
anyNull_neverVerifiesAnynull73     fun anyNull_neverVerifiesAny() {
74         mock<Methods>().apply {
75             nullableString(null)
76             verify(this, never()).nullableString(any())
77         }
78     }
79 
80     @Test
anyNull_verifiesAnyOrNullnull81     fun anyNull_verifiesAnyOrNull() {
82         mock<Methods>().apply {
83             nullableString(null)
84             verify(this).nullableString(anyOrNull())
85         }
86     }
87 
88     @Test
anyNull_forPrimitiveBooleannull89     fun anyNull_forPrimitiveBoolean() {
90         mock<Methods>().apply {
91             boolean(false)
92             verify(this).boolean(anyOrNull())
93         }
94     }
95     @Test
anyNull_forPrimitiveBytenull96     fun anyNull_forPrimitiveByte() {
97         mock<Methods>().apply {
98             byte(3)
99             verify(this).byte(anyOrNull())
100         }
101     }
102 
103     @Test
anyNull_forPrimitiveCharnull104     fun anyNull_forPrimitiveChar() {
105         mock<Methods>().apply {
106             char('a')
107             verify(this).char(anyOrNull())
108         }
109     }
110 
111     @Test
anyNull_forPrimitiveShortnull112     fun anyNull_forPrimitiveShort() {
113         mock<Methods>().apply {
114             short(3)
115             verify(this).short(anyOrNull())
116         }
117     }
118 
119     @Test
anyNull_forPrimitiveIntnull120     fun anyNull_forPrimitiveInt() {
121         mock<Methods>().apply {
122             int(3)
123             verify(this).int(anyOrNull())
124         }
125     }
126 
127     @Test
anyNull_forPrimitiveLongnull128     fun anyNull_forPrimitiveLong() {
129         mock<Methods>().apply {
130             long(3)
131             verify(this).long(anyOrNull())
132         }
133     }
134 
135     @Test
anyNull_forPrimitiveFloatnull136     fun anyNull_forPrimitiveFloat() {
137         mock<Methods>().apply {
138             float(3f)
139             verify(this).float(anyOrNull())
140         }
141     }
142 
143     @Test
anyNull_forPrimitiveDoublenull144     fun anyNull_forPrimitiveDouble() {
145         mock<Methods>().apply {
146             double(3.0)
147             verify(this).double(anyOrNull())
148         }
149     }
150 
151     /** https://github.com/nhaarman/mockito-kotlin/issues/27 */
152     @Test
anyThrowableWithSingleThrowableConstructornull153     fun anyThrowableWithSingleThrowableConstructor() {
154         mock<Methods>().apply {
155             throwableClass(ThrowableClass(IOException()))
156             verify(this).throwableClass(any())
157         }
158     }
159 
160     @Test
listArgThatnull161     fun listArgThat() {
162         mock<Methods>().apply {
163             closedList(listOf(Closed(), Closed()))
164             verify(this).closedList(
165                   argThat {
166                       size == 2
167                   }
168             )
169         }
170     }
171 
172     @Test
listArgForWhichnull173     fun listArgForWhich() {
174         mock<Methods>().apply {
175             closedList(listOf(Closed(), Closed()))
176             verify(this).closedList(
177                   argForWhich {
178                       size == 2
179                   }
180             )
181         }
182     }
183 
184     @Test
listArgWherenull185     fun listArgWhere() {
186         mock<Methods>().apply {
187             closedList(listOf(Closed(), Closed()))
188             verify(this).closedList(
189                   argWhere {
190                       it.size == 2
191                   }
192             )
193         }
194     }
195 
196     @Test
listArgChecknull197     fun listArgCheck() {
198         mock<Methods>().apply {
199             closedList(listOf(Closed(), Closed()))
200             verify(this).closedList(
201                   check {
202                       expect(it.size).toBe(2)
203                   }
204             )
205         }
206     }
207 
208     @Test
checkProperlyFailsnull209     fun checkProperlyFails() {
210         mock<Methods>().apply {
211             closedList(listOf(Closed(), Closed()))
212 
213             expectErrorWithMessage("Argument(s) are different!") on {
214                 verify(this).closedList(
215                       check {
216                           expect(it.size).toBe(1)
217                       }
218                 )
219             }
220         }
221     }
222 
223     @Test
checkWithNullArgument_throwsErrornull224     fun checkWithNullArgument_throwsError() {
225         mock<Methods>().apply {
226             nullableString(null)
227 
228             expectErrorWithMessage("null").on {
229                 verify(this).nullableString(check {})
230             }
231         }
232     }
233 
234 
235     @Test
isA_withNonNullableStringnull236     fun isA_withNonNullableString() {
237         mock<Methods>().apply {
238             string("")
239             verify(this).string(isA<String>())
240         }
241     }
242 
243     @Test
isA_withNullableStringnull244     fun isA_withNullableString() {
245         mock<Methods>().apply {
246             nullableString("")
247             verify(this).nullableString(isA<String>())
248         }
249     }
250 
251     @Test
same_withNonNullArgumentnull252     fun same_withNonNullArgument() {
253         mock<Methods>().apply {
254             string("")
255             verify(this).string(same(""))
256         }
257     }
258 
259     @Test
same_withNullableNonNullArgumentnull260     fun same_withNullableNonNullArgument() {
261         mock<Methods>().apply {
262             nullableString("")
263             verify(this).nullableString(same(""))
264         }
265     }
266 
267     @Test
same_withNullArgumentnull268     fun same_withNullArgument() {
269         mock<Methods>().apply {
270             nullableString(null)
271             verify(this).nullableString(same(null))
272         }
273     }
274 
275     @Test
testVarargAnySuccessnull276     fun testVarargAnySuccess() {
277         /* Given */
278         val t = mock<Methods>()
279         // a matcher to check if any of the varargs was equals to "b"
280         val matcher = VarargAnyMatcher<String, Boolean>({ "b" == it }, true, false)
281 
282         /* When */
283         whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
284 
285         /* Then */
286         expect(t.varargBooleanResult("a", "b", "c")).toBe(true)
287     }
288 
289     @Test
testVarargAnyFailnull290     fun testVarargAnyFail() {
291         /* Given */
292         val t = mock<Methods>()
293         // a matcher to check if any of the varargs was equals to "d"
294         val matcher = VarargAnyMatcher<String, Boolean>({ "d" == it }, true, false)
295 
296         /* When */
297         whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
298 
299         /* Then */
300         expect(t.varargBooleanResult("a", "b", "c")).toBe(false)
301     }
302 
303     /** https://github.com/nhaarman/mockito-kotlin/issues/328 */
304     @Test
testRefEqForNonNullableParameternull305     fun testRefEqForNonNullableParameter() {
306        mock<Methods>().apply {
307            /* When */
308            val array = intArrayOf(2, 3)
309            intArray(array)
310 
311            /* Then */
312            verify(this).intArray(refEq(array))
313        }
314     }
315 
316     /**
317      * a VarargMatcher implementation for varargs of type [T] that will answer with type [R] if any of the var args
318      * matched. Needs to keep state between matching invocations.
319      */
320     private class VarargAnyMatcher<T, R>(
321         private val match: ((T) -> Boolean),
322         private val success: R,
323         private val failure: R
324     ) : ArgumentMatcher<T>, VarargMatcher, Answer<R> {
325         private var anyMatched = false
326 
matchesnull327         override fun matches(t: T): Boolean {
328             anyMatched = anyMatched or match(t)
329             return true
330         }
331 
answernull332         override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
333     }
334 }