Skip to content

Commit

Permalink
Merge pull request #221 from nhaarman/Tapchicoma-improved-spy
Browse files Browse the repository at this point in the history
Add dsl stubbing to spied classes.
  • Loading branch information
nhaarman authored Dec 3, 2017
2 parents 215b383 + 23de85c commit 47424f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ fun <T> reset(vararg mocks: T) = Mockito.reset(*mocks)
fun <T> same(value: T): T = Mockito.same(value) ?: value

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

fun timeout(millis: Long): VerificationWithTimeout = Mockito.timeout(millis)!!
fun times(numInvocations: Int): VerificationMode = Mockito.times(numInvocations)!!
Expand Down
24 changes: 23 additions & 1 deletion mockito-kotlin/src/test/kotlin/test/SpyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SpyTest : TestBase() {
fun doNothingWithSpy() {
val date = spy(Date(0))
doNothing().whenever(date).time = 5L
date.time = 5L;
date.time = 5L
expect(date.time).toBe(0L)
}

Expand All @@ -90,6 +90,28 @@ class SpyTest : TestBase() {
expect(date.time).toBe(0L)
}

@Test
fun doReturnWithDefaultInstanceSpyStubbing() {
val timeVal = 12L

val dateSpy = spy<Date> {
on { time } doReturn timeVal
}

expect(dateSpy.time).toBe(timeVal)
}

@Test
fun doReturnWithSpyStubbing() {
val timeVal = 15L

val dateSpy = spy(Date(0)) {
on { time } doReturn timeVal
}

expect(dateSpy.time).toBe(timeVal)
}

private interface MyInterface
private open class MyClass : MyInterface
private class ClosedClass
Expand Down

0 comments on commit 47424f2

Please sign in to comment.