Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide mock instance to stubbing method in mock() #83

Merged
merged 1 commit into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ fun after(millis: Long) = Mockito.after(millis)

/** Matches any object, excluding nulls. */
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()

/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T = Mockito.any<T>() ?: createInstance<T>()

/** Matches any vararg object, including nulls. */
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()

/** Matches any array of type T. */
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()

inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)

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

inline fun <reified T : Any> mock(stubbing: KStubbing<T>.() -> Unit): T
= Mockito.mock(T::class.java)!!.apply { stubbing(KStubbing(this)) }
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
return mock<T>().apply {
KStubbing(this).stubbing(this)
}
}

class KStubbing<out T>(private val mock: T) {
fun <R> on(methodCall: R) = Mockito.`when`(methodCall)
Expand Down
1 change: 1 addition & 0 deletions mockito-kotlin/src/test/kotlin/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface Methods {
fun nullableString(s: String?)

fun stringResult(): String
fun builderMethod() : Methods
}

class ThrowableClass(cause: Throwable) : Throwable(cause)
14 changes: 14 additions & 0 deletions mockito-kotlin/src/test/kotlin/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ class MockitoTest {
expect(result).toBe("A")
}

@Test
fun testMockStubbing_builder() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() } doReturn mock
}

/* When */
val result = mock.builderMethod()

/* Then */
expect(result).toBeTheSameAs(mock)
}

@Test
fun doReturn_withSingleItemList() {
/* Given */
Expand Down