Skip to content

Commit 1ab7540

Browse files
Add doReturn().on { method() } helper to KStubbing (#453) (#481)
Co-authored-by: Kate Corcoran <kate.corcoran@dfstudio.com>
1 parent 34cb898 commit 1ab7540

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/KStubbing.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import kotlinx.coroutines.runBlocking
3030
import org.mockito.Mockito
3131
import org.mockito.exceptions.misusing.NotAMockException
3232
import org.mockito.stubbing.OngoingStubbing
33+
import org.mockito.stubbing.Stubber
3334
import kotlin.reflect.KClass
3435

3536

@@ -85,4 +86,8 @@ class KStubbing<out T : Any>(val mock: T) {
8586
): OngoingStubbing<R> {
8687
return runBlocking { Mockito.`when`(mock.m()) }
8788
}
89+
90+
fun Stubber.on(methodCall: T.() -> Unit) {
91+
this.`when`(mock).methodCall()
92+
}
8893
}

tests/src/test/kotlin/test/StubberTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,13 @@ class StubberTest : TestBase() {
100100
mock.go()
101101
}
102102
}
103+
104+
@Test
105+
fun testStubberOnBlockExtension() {
106+
val mock = mock<Methods> {
107+
doReturn("Test").on { stringResult() }
108+
}
109+
110+
expect(mock.stringResult()).toBe("Test")
111+
}
103112
}

0 commit comments

Comments
 (0)