Skip to content

Commit

Permalink
Add dsl stubbing to spied classes.
Browse files Browse the repository at this point in the history
Now it is possible to describe spied classes mock behaviour in dsl form as
it was already possible with common mocks.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
  • Loading branch information
Tapchicoma authored and nhaarman committed Dec 3, 2017
1 parent 3bae5f1 commit 23de85c
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 @@ -243,7 +243,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 23de85c

Please sign in to comment.