Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubber (top-level) doSuspendableAnswer() #523

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Stubber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ package org.mockito.kotlin
import kotlinx.coroutines.runBlocking
import org.mockito.Mockito
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.internal.SuspendableAnswer
import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
import kotlin.reflect.KClass

fun <T> doAnswer(answer: (InvocationOnMock) -> T?): Stubber {
return Mockito.doAnswer { answer(it) }!!
}

fun <T> doSuspendableAnswer(answer: suspend (KInvocationOnMock) -> T?): Stubber {
return Mockito.doAnswer(SuspendableAnswer(answer))
}

fun doCallRealMethod(): Stubber {
return Mockito.doCallRealMethod()!!
}
Expand Down
94 changes: 94 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,100 @@ class CoroutinesTest {
}
}

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

doSuspendableAnswer {
withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
}.whenever(fixture).suspendingWithArg(any())

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

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

doSuspendableAnswer {
withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
}.whenever(fixture).suspendingWithArg(any())

val result = async {
val answer = fixture.suspendingWithArg(5)

Result.success(answer)
}

assertEquals(5, result.await().getOrThrow())
}

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

doSuspendableAnswer {
withContext(Dispatchers.Default) { it.getArgument<Int>(0) }
}.whenever(fixture).suspendingWithArg(any())

val actor = actor<Optional<Int>> {
for (element in channel) {
fixture.suspendingWithArg(element.get())
}
}

actor.send(Optional.of(10))
actor.close()

verify(fixture).suspendingWithArg(10)

Unit
}

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

doSuspendableAnswer {
withContext(Dispatchers.Default) { 42 }
}.whenever(fixture).suspending()

assertEquals(42, fixture.suspending())
}

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

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

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

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

val job = Job()

doSuspendableAnswer {
job.join()
5
}.whenever(fixture).suspending()

val asyncTask = async {
fixture.suspending()
}

job.complete()

withTimeout(100) {
assertEquals(5, asyncTask.await())
}
}

@Test
fun inOrderRemainsCompatible() {
/* Given */
Expand Down
Loading