Skip to content

Commit 77f25c1

Browse files
committed
Merge branch 'ramonwirsch-master' into dev
PR #191
2 parents e110a13 + 20a6519 commit 77f25c1

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
package com.nhaarman.mockito_kotlin
2727

2828
import com.nhaarman.mockito_kotlin.createinstance.createInstance
29-
import org.mockito.*
29+
import org.mockito.InOrder
30+
import org.mockito.Incubating
31+
import org.mockito.MockSettings
32+
import org.mockito.MockingDetails
33+
import org.mockito.Mockito
3034
import org.mockito.invocation.InvocationOnMock
3135
import org.mockito.listeners.InvocationListener
3236
import org.mockito.mock.SerializableMode
@@ -193,7 +197,7 @@ class KStubbing<out T>(private val mock: T) {
193197
fun <R : Any> onGeneric(methodCall: T.() -> R, c: KClass<R>): OngoingStubbing<R> {
194198
val r = try {
195199
mock.methodCall()
196-
} catch(e: NullPointerException) {
200+
} catch (e: NullPointerException) {
197201
// An NPE may be thrown by the Kotlin type system when the MockMethodInterceptor returns a
198202
// null value for a non-nullable generic type.
199203
// We catch this NPE to return a valid instance.
@@ -211,7 +215,7 @@ class KStubbing<out T>(private val mock: T) {
211215
fun <R> on(methodCall: T.() -> R): OngoingStubbing<R> {
212216
return try {
213217
Mockito.`when`(mock.methodCall())
214-
} catch(e: NullPointerException) {
218+
} catch (e: NullPointerException) {
215219
throw MockitoKotlinException("NullPointerException thrown when stubbing. If you are trying to stub a generic method, try `onGeneric` instead.", e)
216220
}
217221
}
@@ -277,3 +281,17 @@ fun withSettings(
277281
if (useConstructor) useConstructor()
278282
outerInstance?.let { outerInstance(it) }
279283
}
284+
285+
/**
286+
* Verify multiple calls on mock
287+
* Supports an easier to read style of
288+
*
289+
* ```
290+
* verify(mock) {
291+
* 2 * { call() }
292+
* }
293+
* ```
294+
*/
295+
inline fun <T> verify(mock: T, block: VerifyScope<T>.() -> Unit) {
296+
VerifyScope(mock).block()
297+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.nhaarman.mockito_kotlin
2+
3+
class VerifyScope<T>(val mock: T) {
4+
operator inline fun Int.times(call: T.() -> Unit) {
5+
verify(mock, com.nhaarman.mockito_kotlin.times(this)).call()
6+
}
7+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package test
2+
3+
import com.nhaarman.mockito_kotlin.any
4+
import com.nhaarman.mockito_kotlin.mock
5+
import com.nhaarman.mockito_kotlin.verify
6+
import org.junit.Test
7+
import org.mockito.exceptions.verification.TooLittleActualInvocations
8+
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent
9+
10+
class VerifyTest : TestBase() {
11+
12+
@Test
13+
fun verify0Calls() {
14+
val iface = mock<TestInterface>()
15+
16+
verify(iface) {
17+
0 * { call(any()) }
18+
}
19+
}
20+
21+
@Test
22+
fun verifyNCalls() {
23+
val iface = mock<TestInterface>()
24+
25+
iface.call(42)
26+
iface.call(42)
27+
28+
verify(iface) {
29+
2 * { call(42) }
30+
}
31+
}
32+
33+
@Test(expected = TooLittleActualInvocations::class)
34+
fun verifyFailsWithWrongCount() {
35+
val iface = mock<TestInterface>()
36+
37+
iface.call(0)
38+
39+
verify(iface) {
40+
2 * { call(0) }
41+
}
42+
}
43+
44+
@Test(expected = ArgumentsAreDifferent::class)
45+
fun verifyFailsWithWrongArg() {
46+
val iface = mock<TestInterface>()
47+
48+
iface.call(3)
49+
50+
verify(iface) {
51+
1 * { call(0) }
52+
}
53+
}
54+
55+
interface TestInterface {
56+
fun call(arg: Int)
57+
}
58+
}

0 commit comments

Comments
 (0)