Skip to content

Commit 44cada2

Browse files
authored
Add support for destructured parameters in answers (#512)
1 parent 017b08a commit 44cada2

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 org.mockito.kotlin
27+
28+
import org.mockito.invocation.InvocationOnMock
29+
30+
class KInvocationOnMock(
31+
private val invocationOnMock: InvocationOnMock
32+
) : InvocationOnMock by invocationOnMock {
33+
34+
operator fun <T> component1(): T = invocationOnMock.getArgument(0)
35+
operator fun <T> component2(): T = invocationOnMock.getArgument(1)
36+
operator fun <T> component3(): T = invocationOnMock.getArgument(2)
37+
operator fun <T> component4(): T = invocationOnMock.getArgument(3)
38+
operator fun <T> component5(): T = invocationOnMock.getArgument(4)
39+
}

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/OngoingStubbing.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ package org.mockito.kotlin
2828
import kotlinx.coroutines.CoroutineScope
2929
import kotlinx.coroutines.runBlocking
3030
import org.mockito.Mockito
31-
import org.mockito.invocation.InvocationOnMock
31+
import org.mockito.kotlin.internal.KAnswer
3232
import org.mockito.kotlin.internal.SuspendableAnswer
3333
import org.mockito.stubbing.Answer
3434
import org.mockito.stubbing.OngoingStubbing
@@ -132,10 +132,10 @@ infix fun <T> OngoingStubbing<T>.doAnswer(answer: Answer<*>): OngoingStubbing<T>
132132
/**
133133
* Sets a generic Answer for the method using a lambda.
134134
*/
135-
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> {
136-
return thenAnswer(answer)
135+
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (KInvocationOnMock) -> T?): OngoingStubbing<T> {
136+
return thenAnswer(KAnswer(answer))
137137
}
138138

139-
infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (InvocationOnMock) -> T?): OngoingStubbing<T> {
139+
infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (KInvocationOnMock) -> T?): OngoingStubbing<T> {
140140
return thenAnswer(SuspendableAnswer(answer))
141141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 org.mockito.kotlin.internal
27+
28+
import org.mockito.invocation.InvocationOnMock
29+
import org.mockito.kotlin.KInvocationOnMock
30+
import org.mockito.stubbing.Answer
31+
32+
/**
33+
* This class wraps destructuring lambda into [Answer]
34+
*/
35+
@Suppress("UNCHECKED_CAST")
36+
internal class KAnswer<T>(
37+
private val body: (KInvocationOnMock) -> T?
38+
) : Answer<T> {
39+
override fun answer(invocation: InvocationOnMock): T {
40+
return body(KInvocationOnMock(invocation)) as T
41+
}
42+
}

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/SuspendableAnswer.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package org.mockito.kotlin.internal
2727

2828
import org.mockito.internal.invocation.InterceptedInvocation
2929
import org.mockito.invocation.InvocationOnMock
30+
import org.mockito.kotlin.KInvocationOnMock
3031
import org.mockito.stubbing.Answer
3132
import kotlin.coroutines.Continuation
3233
import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
@@ -36,7 +37,7 @@ import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
3637
*/
3738
@Suppress("UNCHECKED_CAST")
3839
internal class SuspendableAnswer<T>(
39-
private val body: suspend (InvocationOnMock) -> T?
40+
private val body: suspend (KInvocationOnMock) -> T?
4041
) : Answer<T> {
4142
override fun answer(invocation: InvocationOnMock?): T {
4243
//all suspend functions/lambdas has Continuation as the last argument.
@@ -45,6 +46,6 @@ internal class SuspendableAnswer<T>(
4546
val continuation = rawInvocation.rawArguments.last() as Continuation<T?>
4647

4748
// https://youtrack.jetbrains.com/issue/KT-33766#focus=Comments-27-3707299.0-0
48-
return body.startCoroutineUninterceptedOrReturn(invocation, continuation) as T
49+
return body.startCoroutineUninterceptedOrReturn(KInvocationOnMock(invocation), continuation) as T
4950
}
5051
}

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
package test
44

55
import com.nhaarman.expect.expect
6-
import kotlinx.coroutines.Dispatchers
7-
import kotlinx.coroutines.delay
8-
import kotlinx.coroutines.runBlocking
9-
import kotlinx.coroutines.withContext
106
import kotlinx.coroutines.*
117
import kotlinx.coroutines.channels.actor
128
import org.junit.Assert.assertEquals
@@ -265,6 +261,17 @@ class CoroutinesTest {
265261
assertEquals(42, fixture.suspending())
266262
}
267263

264+
@Test
265+
fun answerWithSuspendFunctionWithDestructuredArgs() = runBlocking {
266+
val fixture: SomeInterface = mock()
267+
268+
whenever(fixture.suspendingWithArg(any())).doSuspendableAnswer { (i: Int) ->
269+
withContext(Dispatchers.Default) { i }
270+
}
271+
272+
assertEquals(5, fixture.suspendingWithArg(5))
273+
}
274+
268275
@Test
269276
fun willAnswerWithControlledSuspend() = runBlocking {
270277
val fixture: SomeInterface = mock()

tests/src/test/kotlin/test/OngoingStubbingTest.kt

+30
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,36 @@ class OngoingStubbingTest : TestBase() {
201201
expect(result).toBe("argument-result")
202202
}
203203

204+
@Test
205+
fun testOngoingStubbing_doAnswer_withDestructuredArgument() {
206+
/* Given */
207+
val mock = mock<Methods> {
208+
on { stringResult(any()) } doAnswer { (s: String) -> "$s-result" }
209+
}
210+
211+
/* When */
212+
val result = mock.stringResult("argument")
213+
214+
/* Then */
215+
expect(result).toBe("argument-result")
216+
}
217+
218+
@Test
219+
fun testOngoingStubbing_doAnswer_withDestructuredArguments() {
220+
/* Given */
221+
val mock = mock<Methods> {
222+
on { varargBooleanResult(any(), any()) } doAnswer { (a: String, b: String) ->
223+
a == b.trim()
224+
}
225+
}
226+
227+
/* When */
228+
val result = mock.varargBooleanResult("argument", " argument ")
229+
230+
/* Then */
231+
expect(result).toBe(true)
232+
}
233+
204234
@Test
205235
fun testMockStubbingAfterCreatingMock() {
206236
val mock = mock<Methods>()

0 commit comments

Comments
 (0)