Skip to content

Commit eb0b113

Browse files
authored
Merge pull request #152 from nhaarman/mockstubbing-doanswer
Add doAnswer to OngoingStubbing
2 parents 418d84e + 45e6ce9 commit eb0b113

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ fun <T> OngoingStubbing<T>.doThrow(t: Throwable, vararg ts: Throwable): OngoingS
224224
infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java)
225225
fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>, vararg ts: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java, *ts.map { it.java }.toTypedArray())
226226

227+
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> = thenAnswer(answer)
228+
227229
fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
228230
fun never(): VerificationMode = Mockito.never()!!
229231
fun <T : Any> notNull(): T? = Mockito.notNull()

mockito-kotlin/src/test/kotlin/test/MockitoTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,34 @@ class MockitoTest : TestBase() {
488488
}
489489
}
490490

491+
@Test
492+
fun testMockStubbing_doAnswer() {
493+
/* Given */
494+
val mock = mock<Methods> {
495+
on { stringResult() } doAnswer { "result" }
496+
}
497+
498+
/* When */
499+
val result = mock.stringResult()
500+
501+
/* Then */
502+
expect(result).toBe("result")
503+
}
504+
505+
@Test
506+
fun testMockStubbing_doAnswer_withArgument() {
507+
/* Given */
508+
val mock = mock<Methods> {
509+
on { stringResult(any()) } doAnswer { "${it.arguments[0]}-result" }
510+
}
511+
512+
/* When */
513+
val result = mock.stringResult("argument")
514+
515+
/* Then */
516+
expect(result).toBe("argument-result")
517+
}
518+
491519
@Test
492520
fun mock_withCustomName() {
493521
/* Given */

0 commit comments

Comments
 (0)