Skip to content

Commit

Permalink
Merge pull request #152 from nhaarman/mockstubbing-doanswer
Browse files Browse the repository at this point in the history
Add doAnswer to OngoingStubbing
  • Loading branch information
nhaarman committed Feb 11, 2017
2 parents 418d84e + 45e6ce9 commit eb0b113
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ fun <T> OngoingStubbing<T>.doThrow(t: Throwable, vararg ts: Throwable): OngoingS
infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java)
fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>, vararg ts: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java, *ts.map { it.java }.toTypedArray())

infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> = thenAnswer(answer)

fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
fun never(): VerificationMode = Mockito.never()!!
fun <T : Any> notNull(): T? = Mockito.notNull()
Expand Down
28 changes: 28 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,34 @@ class MockitoTest : TestBase() {
}
}

@Test
fun testMockStubbing_doAnswer() {
/* Given */
val mock = mock<Methods> {
on { stringResult() } doAnswer { "result" }
}

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

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

@Test
fun testMockStubbing_doAnswer_withArgument() {
/* Given */
val mock = mock<Methods> {
on { stringResult(any()) } doAnswer { "${it.arguments[0]}-result" }
}

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

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

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

0 comments on commit eb0b113

Please sign in to comment.