Skip to content

Commit a8c0688

Browse files
authored
Merge pull request #318 from dpreussler/2.x
#317 coroutines verify with verification mode
2 parents ec75b8a + a8072cb commit a8c0688

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/Verification.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ fun <T> verifyBlocking(mock: T, f: suspend T.() -> Unit) {
5353
runBlocking { m.f() }
5454
}
5555

56+
/**
57+
* Verifies certain behavior happened at least once / exact number of times / never.
58+
*
59+
* Warning: Only one method call can be verified in the function.
60+
* Subsequent method calls are ignored!
61+
*/
62+
fun <T> verifyBlocking(mock: T, mode: VerificationMode, f: suspend T.() -> Unit) {
63+
val m = Mockito.verify(mock, mode)
64+
runBlocking { m.f() }
65+
}
5666
/**
5767
* Verifies certain behavior happened at least once / exact number of times / never.
5868
*

mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ class CoroutinesTest {
137137
verifyBlocking(m) { suspending() }
138138
}
139139

140+
@Test
141+
fun verifyAtLeastOnceSuspendFunctionCalled_verifyBlocking() {
142+
val m = mock<SomeInterface>()
143+
144+
runBlocking { m.suspending() }
145+
runBlocking { m.suspending() }
146+
147+
verifyBlocking(m, atLeastOnce()) { suspending() }
148+
}
149+
140150
@Test
141151
fun verifySuspendMethod() = runBlocking {
142152
val testSubject: SomeInterface = mock()

0 commit comments

Comments
 (0)