1 /*
<lambda>null2 * The MIT License
3 *
4 * Copyright (c) 2018 Niek Haarman
5 * Copyright (c) 2007 Mockito contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 package org.mockito.kotlin
27
28 import org.mockito.kotlin.internal.createInstance
29 import kotlinx.coroutines.runBlocking
30 import org.mockito.InOrder
31 import org.mockito.Mockito
32 import org.mockito.kotlin.internal.KInOrderDecorator
33 import org.mockito.verification.VerificationAfterDelay
34 import org.mockito.verification.VerificationMode
35 import org.mockito.verification.VerificationWithTimeout
36
37 /**
38 * Verifies certain behavior <b>happened once</b>.
39 *
40 * Alias for [Mockito.verify].
41 */
42 fun <T> verify(mock: T): T {
43 return Mockito.verify(mock)!!
44 }
45
46 /**
47 * Verifies certain suspending behavior <b>happened once</b>.
48 *
49 * Warning: Only one method call can be verified in the function.
50 * Subsequent method calls are ignored!
51 */
verifyBlockingnull52 fun <T> verifyBlocking(mock: T, f: suspend T.() -> Unit) {
53 val m = Mockito.verify(mock)
54 runBlocking { m.f() }
55 }
56
57 /**
58 * Verifies certain behavior happened at least once / exact number of times / never.
59 *
60 * Warning: Only one method call can be verified in the function.
61 * Subsequent method calls are ignored!
62 */
verifyBlockingnull63 fun <T> verifyBlocking(mock: T, mode: VerificationMode, f: suspend T.() -> Unit) {
64 val m = Mockito.verify(mock, mode)
65 runBlocking { m.f() }
66 }
67 /**
68 * Verifies certain behavior happened at least once / exact number of times / never.
69 *
70 * Alias for [Mockito.verify].
71 */
verifynull72 fun <T> verify(mock: T, mode: VerificationMode): T {
73 return Mockito.verify(mock, mode)!!
74 }
75
76 /**
77 * Checks if any of given mocks has any unverified interaction.
78 *
79 * Alias for [Mockito.verifyNoMoreInteractions].
80 */
verifyNoMoreInteractionsnull81 fun <T> verifyNoMoreInteractions(vararg mocks: T) {
82 Mockito.verifyNoMoreInteractions(*mocks)
83 }
84
85 /**
86 * Verifies that no interactions happened on given mocks beyond the previously verified interactions.
87 *
88 * Alias for [Mockito.verifyNoInteractions].
89 */
verifyNoInteractionsnull90 fun verifyNoInteractions(vararg mocks: Any) {
91 Mockito.verifyNoInteractions(*mocks)
92 }
93
94 /**
95 * Allows verifying exact number of invocations.
96 *
97 * Alias for [Mockito.times].
98 */
timesnull99 fun times(numInvocations: Int): VerificationMode {
100 return Mockito.times(numInvocations)!!
101 }
102
103 /**
104 * Allows at-least-x verification.
105 *
106 * Alias for [Mockito.atLeast].
107 */
atLeastnull108 fun atLeast(numInvocations: Int): VerificationMode {
109 return Mockito.atLeast(numInvocations)!!
110 }
111
112 /**
113 * Allows at-least-once verification.
114 *
115 * Alias for [Mockito.atLeastOnce].
116 */
atLeastOncenull117 fun atLeastOnce(): VerificationMode {
118 return Mockito.atLeastOnce()!!
119 }
120
121 /**
122 * Allows at-most-x verification.
123 *
124 * Alias for [Mockito.atMost].
125 */
atMostnull126 fun atMost(maxNumberOfInvocations: Int): VerificationMode {
127 return Mockito.atMost(maxNumberOfInvocations)!!
128 }
129
130 /**
131 * Allows non-greedy verification in order.
132 *
133 * Alias for [Mockito.calls].
134 */
callsnull135 fun calls(wantedNumberOfInvocations: Int): VerificationMode {
136 return Mockito.calls(wantedNumberOfInvocations)!!
137 }
138
139 /**
140 * Alias for [times] with parameter `0`.
141 */
nevernull142 fun never(): VerificationMode {
143 return Mockito.never()!!
144 }
145
146 /**
147 * Use this method in order to only clear invocations, when stubbing is non-trivial.
148 *
149 * Alias for [Mockito.clearInvocations].
150 */
clearInvocationsnull151 fun <T> clearInvocations(vararg mocks: T) {
152 Mockito.clearInvocations(*mocks)
153 }
154
155 /**
156 * Adds a description to be printed if verification fails.
157 *
158 * Alias for [Mockito.description].
159 */
descriptionnull160 fun description(description: String): VerificationMode {
161 return Mockito.description(description)
162 }
163
164 /**
165 * Allows verifying over a given period. It causes a verify to wait for a specified period of time for a desired
166 * interaction rather than failing immediately if has not already happened. May be useful for testing in concurrent
167 * conditions.
168 */
afternull169 fun after(millis: Long): VerificationAfterDelay {
170 return Mockito.after(millis)!!
171 }
172
173 /**
174 * Allows verifying with timeout. It causes a verify to wait for a specified period of time for a desired
175 * interaction rather than fails immediately if has not already happened. May be useful for testing in concurrent
176 * conditions.
177 */
timeoutnull178 fun timeout(millis: Long): VerificationWithTimeout {
179 return Mockito.timeout(millis)!!
180 }
181
182 /**
183 * Ignores stubbed methods of given mocks for the sake of verification.
184 *
185 * Alias for [Mockito.ignoreStubs].
186 */
ignoreStubsnull187 fun ignoreStubs(vararg mocks: Any): Array<out Any> {
188 return Mockito.ignoreStubs(*mocks)!!
189 }
190
191 /**
192 * Creates [KInOrder] object that allows verifying mocks in order.
193 *
194 * Wrapper for [Mockito.inOrder] that also allows to verify suspending method calls.
195 */
inOrdernull196 fun inOrder(vararg mocks: Any): KInOrder {
197 return KInOrderDecorator(Mockito.inOrder(*mocks)!!)
198 }
199
200 /**
201 * Creates [KInOrder] object that allows verifying mocks in order.
202 * Accepts a lambda to allow easy evaluation.
203 *
204 * Wrapper for [Mockito.inOrder] that also allows to verify suspending method calls.
205 */
inOrdernull206 inline fun inOrder(
207 vararg mocks: Any,
208 evaluation: KInOrder.() -> Unit
209 ) {
210 KInOrderDecorator(Mockito.inOrder(*mocks)).evaluation()
211 }
212
213 /**
214 * Allows [KInOrder] verification for a single mocked instance:
215 *
216 * mock.inOrder {
217 * verify().foo()
218 * verifyBlocking { bar() }
219 * }
220 *
221 */
inOrdernull222 inline fun <T> T.inOrder(block: InOrderOnType<T>.() -> Any) {
223 block.invoke(InOrderOnType(this))
224 }
225
<lambda>null226 class InOrderOnType<T>(private val t: T) : KInOrder by inOrder(t as Any) {
227
228 /**
229 * Verifies certain behavior <b>happened once</b> in order.
230 */
231 fun verify(): T = verify(t)
232
233 /**
234 * Verifies certain behavior happened at least once / exact number of times / never in order.
235 */
236 fun verify(mode: VerificationMode): T = verify(t, mode)
237
238 /**
239 * Verifies certain suspending behavior <b>happened once</b> in order.
240 *
241 * Warning: Only one method call can be verified in the function.
242 * Subsequent method calls are ignored!
243 */
244 fun verifyBlocking(f: suspend T.() -> Unit) = verifyBlocking(t, f)
245
246 /**
247 * Verifies certain suspending behavior happened at least once / exact number of times / never in order.
248 *
249 * Warning: Only one method call can be verified in the function.
250 * Subsequent method calls are ignored!
251 */
252 fun verifyBlocking(mode: VerificationMode, f: suspend T.() -> Unit) = verifyBlocking(t, mode, f)
253 }
254
255 /**
256 * Allows checking if given method was the only one invoked.
257 */
onlynull258 fun only(): VerificationMode {
259 return Mockito.only()!!
260 }
261
262
263 /**
264 * For usage with verification only.
265 *
266 * For example:
267 * verify(myObject).doSomething(check { assertThat(it, is("Test")) })
268 *
269 * @param predicate A function that performs actions to verify an argument [T].
270 */
checknull271 inline fun <reified T : Any> check(noinline predicate: (T) -> Unit): T {
272 return Mockito.argThat { arg: T? ->
273 if (arg == null) error(
274 """The argument passed to the predicate was null.
275
276 If you are trying to verify an argument to be null, use `isNull()`.
277 If you are using `check` as part of a stubbing, use `argThat` or `argForWhich` instead.
278 """.trimIndent()
279 )
280
281 try {
282 predicate(arg)
283 true
284 } catch (e: Error) {
285 e.printStackTrace()
286 false
287 }
288 } ?: createInstance(T::class)
289 }
290