Port missing kotlin specific stubbing methods to BDDMockito (#429)

diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle
index 8385b69..90cdc06 100644
--- a/mockito-kotlin/build.gradle
+++ b/mockito-kotlin/build.gradle
@@ -31,6 +31,7 @@
     testCompile 'com.nhaarman:expect.kt:1.0.0'
 
     testCompile  "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
+    testCompile  "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
     testCompile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'
 
     testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/BDDMockito.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/BDDMockito.kt
index 867f4c1..9363b1e 100644
--- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/BDDMockito.kt
+++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/BDDMockito.kt
@@ -28,7 +28,9 @@
 import org.mockito.BDDMockito
 import org.mockito.BDDMockito.BDDMyOngoingStubbing
 import org.mockito.invocation.InvocationOnMock
+import org.mockito.kotlin.internal.SuspendableAnswer
 import org.mockito.stubbing.Answer
+import kotlin.reflect.KClass
 
 /**
  * Alias for [BDDMockito.given].
@@ -66,6 +68,13 @@
 }
 
 /**
+ * Alias for [BBDMyOngoingStubbing.willAnswer], accepting a suspend lambda.
+ */
+infix fun <T> BDDMyOngoingStubbing<T>.willSuspendableAnswer(value: suspend (InvocationOnMock) -> T?): BDDMockito.BDDMyOngoingStubbing<T> {
+    return willAnswer(SuspendableAnswer(value))
+}
+
+/**
  * Alias for [BBDMyOngoingStubbing.willReturn].
  */
 infix fun <T> BDDMyOngoingStubbing<T>.willReturn(value: () -> T): BDDMockito.BDDMyOngoingStubbing<T> {
@@ -79,3 +88,34 @@
     return willThrow(value())
 }
 
+/**
+ * Sets a Throwable type to be thrown when the method is called.
+ *
+ * Alias for [BDDMyOngoingStubbing.willThrow]
+ */
+infix fun <T> BDDMyOngoingStubbing<T>.willThrow(t: KClass<out Throwable>): BDDMyOngoingStubbing<T> {
+    return willThrow(t.java)
+}
+
+/**
+ * Sets Throwable classes to be thrown when the method is called.
+ *
+ * Alias for [BDDMyOngoingStubbing.willThrow]
+ */
+fun <T> BDDMyOngoingStubbing<T>.willThrow(
+    t: KClass<out Throwable>,
+    vararg ts: KClass<out Throwable>
+): BDDMyOngoingStubbing<T> {
+    return willThrow(t.java, *ts.map { it.java }.toTypedArray())
+}
+
+/**
+ * Sets consecutive return values to be returned when the method is called.
+ * Same as [BDDMyOngoingStubbing.willReturn], but accepts list instead of varargs.
+ */
+inline infix fun <reified T> BDDMyOngoingStubbing<T>.willReturnConsecutively(ts: List<T>): BDDMyOngoingStubbing<T> {
+    return willReturn(
+          ts[0],
+          *ts.drop(1).toTypedArray()
+    )
+}
diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/OngoingStubbing.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/OngoingStubbing.kt
index 78548c8..d259a8f 100644
--- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/OngoingStubbing.kt
+++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/OngoingStubbing.kt
@@ -26,12 +26,10 @@
 package org.mockito.kotlin
 
 import org.mockito.Mockito
-import org.mockito.internal.invocation.InterceptedInvocation
 import org.mockito.invocation.InvocationOnMock
+import org.mockito.kotlin.internal.SuspendableAnswer
 import org.mockito.stubbing.Answer
 import org.mockito.stubbing.OngoingStubbing
-import kotlin.coroutines.Continuation
-import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
 import kotlin.reflect.KClass
 
 
@@ -128,13 +126,5 @@
 }
 
 infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (InvocationOnMock) -> T?): OngoingStubbing<T> {
-    return thenAnswer {
-        //all suspend functions/lambdas has Continuation as the last argument.
-        //InvocationOnMock does not see last argument
-        val rawInvocation = it as InterceptedInvocation
-        val continuation = rawInvocation.rawArguments.last() as Continuation<T?>
-
-        // https://youtrack.jetbrains.com/issue/KT-33766#focus=Comments-27-3707299.0-0
-        answer.startCoroutineUninterceptedOrReturn(it, continuation)
-    }
+    return thenAnswer(SuspendableAnswer(answer))
 }
diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/SuspendableAnswer.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/SuspendableAnswer.kt
new file mode 100644
index 0000000..3544cf6
--- /dev/null
+++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/SuspendableAnswer.kt
@@ -0,0 +1,50 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2018 Niek Haarman
+ * Copyright (c) 2007 Mockito contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package org.mockito.kotlin.internal
+
+import org.mockito.internal.invocation.InterceptedInvocation
+import org.mockito.invocation.InvocationOnMock
+import org.mockito.stubbing.Answer
+import kotlin.coroutines.Continuation
+import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
+
+/**
+ * This class properly wraps suspendable lambda into [Answer]
+ */
+@Suppress("UNCHECKED_CAST")
+internal class SuspendableAnswer<T>(
+    private val body: suspend (InvocationOnMock) -> T?
+) : Answer<T> {
+    override fun answer(invocation: InvocationOnMock?): T {
+        //all suspend functions/lambdas has Continuation as the last argument.
+        //InvocationOnMock does not see last argument
+        val rawInvocation = invocation as InterceptedInvocation
+        val continuation = rawInvocation.rawArguments.last() as Continuation<T?>
+
+        // https://youtrack.jetbrains.com/issue/KT-33766#focus=Comments-27-3707299.0-0
+        return body.startCoroutineUninterceptedOrReturn(invocation, continuation) as T
+    }
+}
diff --git a/mockito-kotlin/src/test/kotlin/org/mockito/kotlin/BDDMockitoKtTest.kt b/mockito-kotlin/src/test/kotlin/org/mockito/kotlin/BDDMockitoKtTest.kt
new file mode 100644
index 0000000..0686efe
--- /dev/null
+++ b/mockito-kotlin/src/test/kotlin/org/mockito/kotlin/BDDMockitoKtTest.kt
@@ -0,0 +1,79 @@
+package org.mockito.kotlin
+
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.withContext
+import org.junit.Assert.assertEquals
+import org.junit.Test
+import kotlin.test.assertFailsWith
+
+class BDDMockitoKtTest {
+
+    @Test
+    fun willSuspendableAnswer_withoutArgument() = runBlocking {
+        val fixture: SomeInterface = mock()
+
+        given(fixture.suspending()).willSuspendableAnswer {
+            withContext(Dispatchers.Default) { 42 }
+        }
+
+        assertEquals(42, fixture.suspending())
+        then(fixture).should().suspending()
+        Unit
+    }
+
+    @Test
+    fun willSuspendableAnswer_witArgument() = runBlocking {
+        val fixture: SomeInterface = mock()
+
+        given(fixture.suspendingWithArg(any())).willSuspendableAnswer {
+            withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
+        }
+
+        assertEquals(42, fixture.suspendingWithArg(42))
+        then(fixture).should().suspendingWithArg(42)
+        Unit
+    }
+
+    @Test
+    fun willThrow_kclass_single() {
+        val fixture: SomeInterface = mock()
+
+        given(fixture.foo()).willThrow(RuntimeException::class)
+
+        assertFailsWith(RuntimeException::class) {
+            fixture.foo()
+        }
+    }
+
+    @Test
+    fun willThrow_kclass_multiple() {
+        val fixture: SomeInterface = mock()
+
+        given(fixture.foo()).willThrow(RuntimeException::class, IllegalArgumentException::class)
+
+        assertFailsWith(RuntimeException::class) {
+            fixture.foo()
+        }
+        assertFailsWith(IllegalArgumentException::class) {
+            fixture.foo()
+        }
+    }
+
+    @Test
+    fun willReturnConsecutively() {
+        val fixture: SomeInterface = mock()
+
+        given(fixture.foo()).willReturnConsecutively(listOf(42, 24))
+
+        assertEquals(42, fixture.foo())
+        assertEquals(24, fixture.foo())
+    }
+}
+
+interface SomeInterface {
+    fun foo(): Int
+
+    suspend fun suspending(): Int
+    suspend fun suspendingWithArg(arg: Int): Int
+}