Skip to content

Commit

Permalink
Add support for destructured parameters in answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Osguima3 committed Feb 23, 2024
1 parent 017b08a commit ceedf0d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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

import org.mockito.invocation.InvocationOnMock

class KInvocationOnMock(
private val invocationOnMock: InvocationOnMock
) : InvocationOnMock by invocationOnMock {

operator fun <T> component1(): T = invocationOnMock.getArgument(0)
operator fun <T> component2(): T = invocationOnMock.getArgument(1)
operator fun <T> component3(): T = invocationOnMock.getArgument(2)
operator fun <T> component4(): T = invocationOnMock.getArgument(3)
operator fun <T> component5(): T = invocationOnMock.getArgument(4)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package org.mockito.kotlin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import org.mockito.Mockito
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.internal.KAnswer
import org.mockito.kotlin.internal.SuspendableAnswer
import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
Expand Down Expand Up @@ -132,10 +132,10 @@ infix fun <T> OngoingStubbing<T>.doAnswer(answer: Answer<*>): OngoingStubbing<T>
/**
* Sets a generic Answer for the method using a lambda.
*/
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> {
return thenAnswer(answer)
infix fun <T> OngoingStubbing<T>.doAnswer(answer: (KInvocationOnMock) -> T?): OngoingStubbing<T> {
return thenAnswer(KAnswer(answer))
}

infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (InvocationOnMock) -> T?): OngoingStubbing<T> {
infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (KInvocationOnMock) -> T?): OngoingStubbing<T> {
return thenAnswer(SuspendableAnswer(answer))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.invocation.InvocationOnMock
import org.mockito.kotlin.KInvocationOnMock
import org.mockito.stubbing.Answer

/**
* This class wraps destructuring lambda into [Answer]
*/
@Suppress("UNCHECKED_CAST")
internal class KAnswer<T>(
private val body: (KInvocationOnMock) -> T?
) : Answer<T> {
override fun answer(invocation: InvocationOnMock): T {
return body(KInvocationOnMock(invocation)) as T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package org.mockito.kotlin.internal

import org.mockito.internal.invocation.InterceptedInvocation
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.KInvocationOnMock
import org.mockito.stubbing.Answer
import kotlin.coroutines.Continuation
import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
Expand All @@ -36,7 +37,7 @@ import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
*/
@Suppress("UNCHECKED_CAST")
internal class SuspendableAnswer<T>(
private val body: suspend (InvocationOnMock) -> T?
private val body: suspend (KInvocationOnMock) -> T?
) : Answer<T> {
override fun answer(invocation: InvocationOnMock?): T {
//all suspend functions/lambdas has Continuation as the last argument.
Expand All @@ -45,6 +46,6 @@ internal class SuspendableAnswer<T>(
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
return body.startCoroutineUninterceptedOrReturn(KInvocationOnMock(invocation), continuation) as T
}
}
15 changes: 11 additions & 4 deletions mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
package test

import com.nhaarman.expect.expect
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.actor
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -265,6 +261,17 @@ class CoroutinesTest {
assertEquals(42, fixture.suspending())
}

@Test
fun answerWithSuspendFunctionWithDestructuredArgs() = runBlocking {
val fixture: SomeInterface = mock()

whenever(fixture.suspendingWithArg(any())).doSuspendableAnswer { (i: Int) ->
withContext(Dispatchers.Default) { i }
}

assertEquals(5, fixture.suspendingWithArg(5))
}

@Test
fun willAnswerWithControlledSuspend() = runBlocking {
val fixture: SomeInterface = mock()
Expand Down
30 changes: 30 additions & 0 deletions tests/src/test/kotlin/test/OngoingStubbingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ class OngoingStubbingTest : TestBase() {
expect(result).toBe("argument-result")
}

@Test
fun testOngoingStubbing_doAnswer_withDestructuredArgument() {
/* Given */
val mock = mock<Methods> {
on { stringResult(any()) } doAnswer { (s: String) -> "$s-result" }
}

/* When */
val result = mock.stringResult("argument")

/* Then */
expect(result).toBe("argument-result")
}

@Test
fun testOngoingStubbing_doAnswer_withDestructuredArguments() {
/* Given */
val mock = mock<Methods> {
on { varargBooleanResult(any(), any()) } doAnswer { (a: String, b: String) ->
a == b.trim()
}
}

/* When */
val result = mock.varargBooleanResult("argument", " argument ")

/* Then */
expect(result).toBe(true)
}

@Test
fun testMockStubbingAfterCreatingMock() {
val mock = mock<Methods>()
Expand Down

0 comments on commit ceedf0d

Please sign in to comment.