Skip to content

Commit 1025568

Browse files
authored
Merge pull request #83 from nhaarman/stubbing-mock-reference
Provide mock instance to stubbing method in mock()
2 parents 3f73e27 + 0c3e137 commit 1025568

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ fun after(millis: Long) = Mockito.after(millis)
4141

4242
/** Matches any object, excluding nulls. */
4343
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
44+
4445
/** Matches anything, including nulls. */
4546
inline fun <reified T : Any> anyOrNull(): T = Mockito.any<T>() ?: createInstance<T>()
47+
4648
/** Matches any vararg object, including nulls. */
4749
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()
50+
4851
/** Matches any array of type T. */
4952
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
53+
5054
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
5155
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)
5256

@@ -80,8 +84,11 @@ inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(
8084
inline fun <reified T : Any> mock(s: MockSettings): T = Mockito.mock(T::class.java, s)!!
8185
inline fun <reified T : Any> mock(s: String): T = Mockito.mock(T::class.java, s)!!
8286

83-
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.() -> Unit): T
84-
= Mockito.mock(T::class.java)!!.apply { stubbing(KStubbing(this)) }
87+
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
88+
return mock<T>().apply {
89+
KStubbing(this).stubbing(this)
90+
}
91+
}
8592

8693
class KStubbing<out T>(private val mock: T) {
8794
fun <R> on(methodCall: R) = Mockito.`when`(methodCall)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ interface Methods {
5454
fun nullableString(s: String?)
5555

5656
fun stringResult(): String
57+
fun builderMethod() : Methods
5758
}
5859

5960
class ThrowableClass(cause: Throwable) : Throwable(cause)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,20 @@ class MockitoTest {
361361
expect(result).toBe("A")
362362
}
363363

364+
@Test
365+
fun testMockStubbing_builder() {
366+
/* Given */
367+
val mock = mock<Methods> { mock ->
368+
on { builderMethod() } doReturn mock
369+
}
370+
371+
/* When */
372+
val result = mock.builderMethod()
373+
374+
/* Then */
375+
expect(result).toBeTheSameAs(mock)
376+
}
377+
364378
@Test
365379
fun doReturn_withSingleItemList() {
366380
/* Given */

0 commit comments

Comments
 (0)