Skip to content

Commit

Permalink
Add doReturn().on { method() } helper to KStubbing (mockito#453) (m…
Browse files Browse the repository at this point in the history
…ockito#481)

Co-authored-by: Kate Corcoran <kate.corcoran@dfstudio.com>
  • Loading branch information
thecodewarrior and Kate Corcoran authored Mar 17, 2023
1 parent 34cb898 commit 1ab7540
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlinx.coroutines.runBlocking
import org.mockito.Mockito
import org.mockito.exceptions.misusing.NotAMockException
import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
import kotlin.reflect.KClass


Expand Down Expand Up @@ -85,4 +86,8 @@ class KStubbing<out T : Any>(val mock: T) {
): OngoingStubbing<R> {
return runBlocking { Mockito.`when`(mock.m()) }
}

fun Stubber.on(methodCall: T.() -> Unit) {
this.`when`(mock).methodCall()
}
}
9 changes: 9 additions & 0 deletions tests/src/test/kotlin/test/StubberTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ class StubberTest : TestBase() {
mock.go()
}
}

@Test
fun testStubberOnBlockExtension() {
val mock = mock<Methods> {
doReturn("Test").on { stringResult() }
}

expect(mock.stringResult()).toBe("Test")
}
}

0 comments on commit 1ab7540

Please sign in to comment.