Skip to content

Commit

Permalink
Merge pull request #220 from nhaarman/stubbing-existing-mock
Browse files Browse the repository at this point in the history
Add function to stub an existing mock with lambda syntax
  • Loading branch information
nhaarman authored Dec 3, 2017
2 parents 936a3fc + 688f90b commit 215b383
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@

package com.nhaarman.mockitokotlin2

import org.mockito.InOrder
import org.mockito.Incubating
import org.mockito.MockSettings
import org.mockito.MockingDetails
import org.mockito.Mockito
import org.mockito.*
import org.mockito.invocation.InvocationOnMock
import org.mockito.listeners.InvocationListener
import org.mockito.mock.SerializableMode
import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
import org.mockito.verification.VerificationMode
import org.mockito.verification.VerificationWithTimeout
import org.mockito.stubbing.*
import org.mockito.verification.*
import kotlin.DeprecationLevel.WARNING
import kotlin.reflect.KClass

Expand Down Expand Up @@ -178,6 +171,8 @@ inline fun <reified T : Any> mock(
KStubbing(this).stubbing(this)
}!!

inline fun <T> stubbing(mock: T, stubbing: KStubbing<T>.(T) -> Unit) = KStubbing(mock).stubbing(mock)

inline fun <T : Any> T.stub(stubbing: KStubbing<T>.(T) -> Unit) = this.apply { KStubbing(this).stubbing(this) }

@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock<T>(defaultAnswer = a)"), level = WARNING)
Expand Down
14 changes: 14 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1052,4 +1052,18 @@ class MockitoTest : TestBase() {
verify(this).nullableString(same(null))
}
}

@Test
fun stubbingExistingMock() {
/* Given */
val mock = mock<Methods>()

/* When */
stubbing(mock) {
on { stringResult() } doReturn "result"
}

/* Then */
expect(mock.stringResult()).toBe("result")
}
}

0 comments on commit 215b383

Please sign in to comment.