Skip to content

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018 Niek Haarman
5+
* Copyright (c) 2007 Mockito contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.nhaarman.mockitokotlin2.internal;
27+
28+
import org.jetbrains.annotations.NotNull;
29+
import org.jetbrains.annotations.Nullable;
30+
import org.mockito.invocation.InvocationOnMock;
31+
32+
import kotlin.coroutines.Continuation;
33+
import kotlin.jvm.functions.Function2;
34+
35+
/**
36+
* This wrapper is needed, because kotlin does not allow explicit call suspend function providing Continuation.
37+
* But interoperability via Java allows it.
38+
*/
39+
public class JavaSuspendWrapper {
40+
/**
41+
* @param function accepting Object, otherwise kotlin does not allow pass suspending lambda
42+
*/
43+
@Nullable
44+
public static <T> Object wrapSuspend(@NotNull Continuation<?> continuation, @NotNull InvocationOnMock invocationOnMock, @NotNull Object function) {
45+
Function2<InvocationOnMock, Continuation<? super T>, Object> realFunction = (Function2<InvocationOnMock, Continuation<? super T>, Object>) function;
46+
return KotlinSuspendWrapperKt.wrapSuspend(realFunction, invocationOnMock, (Continuation<Object>) continuation);
47+
}
48+
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
package com.nhaarman.mockitokotlin2
2727

28+
import com.nhaarman.mockitokotlin2.internal.JavaSuspendWrapper
2829
import org.mockito.Mockito
30+
import org.mockito.internal.invocation.InterceptedInvocation
2931
import org.mockito.invocation.InvocationOnMock
3032
import org.mockito.stubbing.Answer
3133
import org.mockito.stubbing.OngoingStubbing
32-
import kotlin.DeprecationLevel.ERROR
34+
import kotlin.coroutines.Continuation
3335
import kotlin.reflect.KClass
3436

3537

@@ -124,3 +126,15 @@ infix fun <T> OngoingStubbing<T>.doAnswer(answer: Answer<*>): OngoingStubbing<T>
124126
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> {
125127
return thenAnswer(answer)
126128
}
129+
130+
infix fun <T> OngoingStubbing<T>.willAnswer(answer: suspend (InvocationOnMock) -> T?): OngoingStubbing<T> {
131+
return doAnswer {
132+
//all suspend functions/lambdas has Continuation as the last argument.
133+
//InvocationOnMock does not see last argument
134+
val rawInvocation = it as InterceptedInvocation
135+
val continuation = rawInvocation.rawArguments[1] as Continuation<*>
136+
137+
//This method sometimes returns different object. T? is needed to make it compile.
138+
JavaSuspendWrapper.wrapSuspend<T>(continuation, it, answer) as T?
139+
}
140+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018 Niek Haarman
5+
* Copyright (c) 2007 Mockito contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.nhaarman.mockitokotlin2.internal
27+
28+
import org.mockito.invocation.InvocationOnMock
29+
import kotlin.coroutines.Continuation
30+
import kotlin.coroutines.suspendCoroutine
31+
32+
suspend fun <T> wrapSuspend(
33+
body: Function2<InvocationOnMock, Continuation<T>, T>,
34+
invocationOnMock: InvocationOnMock
35+
): T {
36+
//suspendCoroutine is needed to create new continuation.
37+
return suspendCoroutine { continuation ->
38+
//body is suspend lambda. From Java we got is as regular function so we could simple call it by providing own Continuation
39+
body(invocationOnMock, continuation)
40+
}
41+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.coroutines.Dispatchers
88
import kotlinx.coroutines.delay
99
import kotlinx.coroutines.runBlocking
1010
import kotlinx.coroutines.withContext
11+
import org.junit.Assert.assertEquals
1112
import org.junit.Test
1213

1314

@@ -157,11 +158,34 @@ class CoroutinesTest {
157158
verify(testSubject).suspending()
158159
}
159160
}
161+
162+
@Test
163+
fun answerWithSuspendFunction() = runBlocking {
164+
val fixture: SomeInterface = mock()
165+
166+
whenever(fixture.suspendingWithArg(any())).willAnswer {
167+
withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
168+
}
169+
170+
assertEquals(5, fixture.suspendingWithArg(5))
171+
}
172+
173+
@Test
174+
fun inplaceAnswerWithSuspendFunction() = runBlocking {
175+
val fixture: SomeInterface = mock {
176+
onBlocking { suspendingWithArg(any()) } willAnswer {
177+
withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
178+
}
179+
}
180+
181+
assertEquals(5, fixture.suspendingWithArg(5))
182+
}
160183
}
161184

162185
interface SomeInterface {
163186

164187
suspend fun suspending(): Int
188+
suspend fun suspendingWithArg(arg: Int): Int
165189
fun nonsuspending(): Int
166190
}
167191

0 commit comments

Comments
 (0)