Skip to content

Commit 9cd2095

Browse files
authored
Merge pull request #216 from nhaarman/doAnswer
Implement OngoingStubbing<T>.doAnswer(Answer<T>)
2 parents c4cb85e + 719cd29 commit 9cd2095

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sat Nov 25 15:58:11 CET 2017
1+
#Sun Dec 03 21:34:47 CET 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-all.zip

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubb
230230
fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>, vararg ts: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java, *ts.map { it.java }.toTypedArray())
231231

232232
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> = thenAnswer(answer)
233+
infix fun <T> OngoingStubbing<T>.doAnswer(answer: Answer<T>): OngoingStubbing<T> = thenAnswer(answer)
233234

234235
fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
235236
fun never(): VerificationMode = Mockito.never()!!

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package test
22

3-
import com.nhaarman.expect.expect
4-
import com.nhaarman.expect.expectErrorWithMessage
5-
import com.nhaarman.expect.fail
3+
import com.nhaarman.expect.*
64
import com.nhaarman.mockitokotlin2.*
75
import org.junit.Test
86
import org.mockito.Mockito
@@ -11,9 +9,8 @@ import org.mockito.exceptions.base.MockitoAssertionError
119
import org.mockito.exceptions.verification.WantedButNotInvoked
1210
import org.mockito.listeners.InvocationListener
1311
import org.mockito.mock.SerializableMode.BASIC
14-
import java.io.IOException
15-
import java.io.PrintStream
16-
import java.io.Serializable
12+
import org.mockito.stubbing.Answer
13+
import java.io.*
1714

1815

1916
/*
@@ -327,7 +324,7 @@ class MockitoTest : TestBase() {
327324
try {
328325
mock.go()
329326
throw AssertionError("Call should have thrown.")
330-
} catch(e: IllegalStateException) {
327+
} catch (e: IllegalStateException) {
331328
}
332329
}
333330

@@ -425,7 +422,7 @@ class MockitoTest : TestBase() {
425422
/* When */
426423
mock.builderMethod()
427424
fail("No exception thrown")
428-
} catch(e: IllegalArgumentException) {
425+
} catch (e: IllegalArgumentException) {
429426
}
430427
}
431428

@@ -440,7 +437,7 @@ class MockitoTest : TestBase() {
440437
/* When */
441438
mock.builderMethod()
442439
fail("No exception thrown")
443-
} catch(e: IllegalArgumentException) {
440+
} catch (e: IllegalArgumentException) {
444441
}
445442
}
446443

@@ -455,14 +452,14 @@ class MockitoTest : TestBase() {
455452
/* When */
456453
mock.builderMethod()
457454
fail("No exception thrown")
458-
} catch(e: IllegalArgumentException) {
455+
} catch (e: IllegalArgumentException) {
459456
}
460457

461458
try {
462459
/* When */
463460
mock.builderMethod()
464461
fail("No exception thrown")
465-
} catch(e: UnsupportedOperationException) {
462+
} catch (e: UnsupportedOperationException) {
466463
}
467464
}
468465

@@ -477,19 +474,19 @@ class MockitoTest : TestBase() {
477474
/* When */
478475
mock.builderMethod()
479476
fail("No exception thrown")
480-
} catch(e: IllegalArgumentException) {
477+
} catch (e: IllegalArgumentException) {
481478
}
482479

483480
try {
484481
/* When */
485482
mock.builderMethod()
486483
fail("No exception thrown")
487-
} catch(e: UnsupportedOperationException) {
484+
} catch (e: UnsupportedOperationException) {
488485
}
489486
}
490487

491488
@Test
492-
fun testMockStubbing_doAnswer() {
489+
fun testMockStubbing_doAnswer_lambda() {
493490
/* Given */
494491
val mock = mock<Methods> {
495492
on { stringResult() } doAnswer { "result" }
@@ -502,6 +499,21 @@ class MockitoTest : TestBase() {
502499
expect(result).toBe("result")
503500
}
504501

502+
@Test
503+
fun testMockStubbing_doAnswer_instance() {
504+
/* Given */
505+
val mock = mock<Methods> {
506+
on { stringResult() } doAnswer Answer<String> { "result" }
507+
}
508+
509+
/* When */
510+
val result = mock.stringResult()
511+
512+
/* Then */
513+
expect(result).toBe("result")
514+
}
515+
516+
505517
@Test
506518
fun testMockStubbing_doAnswer_withArgument() {
507519
/* Given */
@@ -659,7 +671,7 @@ class MockitoTest : TestBase() {
659671
/* When */
660672
verify(mock).stringResult()
661673
fail("Expected an exception")
662-
} catch(e: WantedButNotInvoked) {
674+
} catch (e: WantedButNotInvoked) {
663675
/* Then */
664676
verify(out).println("methods.stringResult();")
665677
}
@@ -768,7 +780,7 @@ class MockitoTest : TestBase() {
768780
/* When */
769781
verify(mock).stringResult()
770782
fail("Expected an exception")
771-
} catch(e: WantedButNotInvoked) {
783+
} catch (e: WantedButNotInvoked) {
772784
/* Then */
773785
verify(out).println("methods.stringResult();")
774786
}
@@ -869,7 +881,7 @@ class MockitoTest : TestBase() {
869881
/* When */
870882
verify(mock).stringResult()
871883
fail("Expected an exception")
872-
} catch(e: WantedButNotInvoked) {
884+
} catch (e: WantedButNotInvoked) {
873885
/* Then */
874886
verify(out).println("methods.stringResult();")
875887
}

0 commit comments

Comments
 (0)