Skip to content

Commit

Permalink
Merge pull request #158 from pbochenski/master
Browse files Browse the repository at this point in the history
Added stub extension function
  • Loading branch information
nhaarman committed Apr 1, 2017
2 parents 4c7c942 + 3349ebd commit 0b22ec3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ inline fun <reified T : Any> mock(
KStubbing(this).stubbing(this)
}!!

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)
inline fun <reified T : Any> mock(a: Answer<Any>): T = mock(defaultAnswer = a)

Expand Down
35 changes: 35 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,41 @@ class MockitoTest : TestBase() {
expect(result).toBe("argument-result")
}

@Test
fun testMockStubbingAfterCreatingMock() {
val mock = mock<Methods>()

//create stub after creation of mock
mock.stub {
on { stringResult() } doReturn "result"
}

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

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

@Test
fun testOverrideDefaultStub() {
/* Given mock with stub */
val mock = mock<Methods> {
on { stringResult() } doReturn "result1"
}

/* override stub */
mock.stub {
on { stringResult() } doReturn "result2"
}

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

/* Then */
expect(result).toBe("result2")
}

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

0 comments on commit 0b22ec3

Please sign in to comment.