Best Mockito-kotlin code snippet using org.mockito.kotlin.internal.SuspendableAnswer
OngoingStubbing.kt
Source:OngoingStubbing.kt
...24 */25package org.mockito.kotlin26import org.mockito.Mockito27import org.mockito.invocation.InvocationOnMock28import org.mockito.kotlin.internal.SuspendableAnswer29import org.mockito.stubbing.Answer30import org.mockito.stubbing.OngoingStubbing31import kotlin.reflect.KClass32/**33 * Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.34 *35 * Alias for [Mockito.when].36 */37@Suppress("NOTHING_TO_INLINE")38inline fun <T> whenever(methodCall: T): OngoingStubbing<T> {39 return Mockito.`when`(methodCall)!!40}41/**42 * Sets a return value to be returned when the method is called.43 *44 * Alias for [OngoingStubbing.thenReturn].45 */46infix fun <T> OngoingStubbing<T>.doReturn(t: T): OngoingStubbing<T> {47 return thenReturn(t)48}49/**50 * Sets consecutive return values to be returned when the method is called.51 *52 * Alias for [OngoingStubbing.thenReturn].53 */54fun <T> OngoingStubbing<T>.doReturn(t: T, vararg ts: T): OngoingStubbing<T> {55 return thenReturn(t, *ts)56}57/**58 * Sets consecutive return values to be returned when the method is called.59 */60inline infix fun <reified T> OngoingStubbing<T>.doReturnConsecutively(ts: List<T>): OngoingStubbing<T> {61 return thenReturn(62 ts[0],63 *ts.drop(1).toTypedArray()64 )65}66/**67 * Sets Throwable objects to be thrown when the method is called.68 *69 * Alias for [OngoingStubbing.thenThrow].70 */71infix fun <T> OngoingStubbing<T>.doThrow(t: Throwable): OngoingStubbing<T> {72 return thenThrow(t)73}74/**75 * Sets Throwable objects to be thrown when the method is called.76 *77 * Alias for [OngoingStubbing.doThrow].78 */79fun <T> OngoingStubbing<T>.doThrow(80 t: Throwable,81 vararg ts: Throwable82): OngoingStubbing<T> {83 return thenThrow(t, *ts)84}85/**86 * Sets a Throwable type to be thrown when the method is called.87 */88infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubbing<T> {89 return thenThrow(t.java)90}91/**92 * Sets Throwable classes to be thrown when the method is called.93 */94fun <T> OngoingStubbing<T>.doThrow(95 t: KClass<out Throwable>,96 vararg ts: KClass<out Throwable>97): OngoingStubbing<T> {98 return thenThrow(t.java, *ts.map { it.java }.toTypedArray())99}100/**101 * Sets a generic Answer for the method.102 *103 * Alias for [OngoingStubbing.thenAnswer].104 */105infix fun <T> OngoingStubbing<T>.doAnswer(answer: Answer<*>): OngoingStubbing<T> {106 return thenAnswer(answer)107}108/**109 * Sets a generic Answer for the method using a lambda.110 */111infix fun <T> OngoingStubbing<T>.doAnswer(answer: (InvocationOnMock) -> T?): OngoingStubbing<T> {112 return thenAnswer(answer)113}114infix fun <T> OngoingStubbing<T>.doSuspendableAnswer(answer: suspend (InvocationOnMock) -> T?): OngoingStubbing<T> {115 return thenAnswer(SuspendableAnswer(answer))116}...
BDDMockito.kt
Source:BDDMockito.kt
...25package org.mockito.kotlin26import org.mockito.BDDMockito27import org.mockito.BDDMockito.BDDMyOngoingStubbing28import org.mockito.invocation.InvocationOnMock29import org.mockito.kotlin.internal.SuspendableAnswer30import org.mockito.stubbing.Answer31import kotlin.reflect.KClass32/**33 * Alias for [BDDMockito.given].34 */35fun <T> given(methodCall: T): BDDMockito.BDDMyOngoingStubbing<T> {36 return BDDMockito.given(methodCall)37}38/**39 * Alias for [BDDMockito.given] with a lambda.40 */41fun <T> given(methodCall: () -> T): BDDMyOngoingStubbing<T> {42 return given(methodCall())43}44/**45 * Alias for [BDDMockito.then].46 */47fun <T> then(mock: T): BDDMockito.Then<T> {48 return BDDMockito.then(mock)49}50/**51 * Alias for [BDDMyOngoingStubbing.will]52 * */53infix fun <T> BDDMyOngoingStubbing<T>.will(value: Answer<T>): BDDMockito.BDDMyOngoingStubbing<T> {54 return will(value)55}56/**57 * Alias for [BBDMyOngoingStubbing.willAnswer], accepting a lambda.58 */59infix fun <T> BDDMyOngoingStubbing<T>.willAnswer(value: (InvocationOnMock) -> T?): BDDMockito.BDDMyOngoingStubbing<T> {60 return willAnswer { value(it) }61}62/**63 * Alias for [BBDMyOngoingStubbing.willAnswer], accepting a suspend lambda.64 */65infix fun <T> BDDMyOngoingStubbing<T>.willSuspendableAnswer(value: suspend (InvocationOnMock) -> T?): BDDMockito.BDDMyOngoingStubbing<T> {66 return willAnswer(SuspendableAnswer(value))67}68/**69 * Alias for [BBDMyOngoingStubbing.willReturn].70 */71infix fun <T> BDDMyOngoingStubbing<T>.willReturn(value: () -> T): BDDMockito.BDDMyOngoingStubbing<T> {72 return willReturn(value())73}74/**75 * Alias for [BBDMyOngoingStubbing.willThrow].76 */77infix fun <T> BDDMyOngoingStubbing<T>.willThrow(value: () -> Throwable): BDDMockito.BDDMyOngoingStubbing<T> {78 return willThrow(value())79}80/**...
SuspendableAnswer.kt
Source:SuspendableAnswer.kt
...31/**32 * This class properly wraps suspendable lambda into [Answer]33 */34@Suppress("UNCHECKED_CAST")35internal class SuspendableAnswer<T>(36 private val body: suspend (InvocationOnMock) -> T?37) : Answer<T> {38 override fun answer(invocation: InvocationOnMock?): T {39 //all suspend functions/lambdas has Continuation as the last argument.40 //InvocationOnMock does not see last argument41 val rawInvocation = invocation as InterceptedInvocation42 val continuation = rawInvocation.rawArguments.last() as Continuation<T?>43 // https://youtrack.jetbrains.com/issue/KT-33766#focus=Comments-27-3707299.0-044 return body.startCoroutineUninterceptedOrReturn(invocation, continuation) as T45 }46}...
SuspendableAnswer
Using AI Code Generation
1suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Any?): Answer<Any?> = suspendableAnswer(answer)2suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Unit): Answer<Unit> = suspendableAnswer(answer)3suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Nothing): Answer<Nothing> = suspendableAnswer(answer)4suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Unit): Answer<Unit> = suspendableAnswer(answer)5suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Nothing): Answer<Nothing> = suspendableAnswer(answer)6suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Unit): Answer<Unit> = suspendableAnswer(answer)7suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Nothing): Answer<Nothing> = suspendableAnswer(answer)8suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Unit): Answer<Unit> = suspendableAnswer(answer)9suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Nothing): Answer<Nothing> = suspendableAnswer(answer)10suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Unit): Answer<Unit> = suspendableAnswer(answer)11suspend fun CoroutineScope.suspendableAnswer(answer: suspend (InvocationOnMock) -> Nothing): Answer<Nothing> = suspendableAnswer(answer)12suspend fun CoroutineScope.suspendableAnswer(answer
SuspendableAnswer
Using AI Code Generation
1 fun test() {2 val mock = mock<MyInterface>()3 val mock2 = mock<MyInterface>()4 val mock3 = mock<MyInterface>()5 val mock4 = mock<MyInterface>()6 val answer = SuspendableAnswer<Unit> {7 println("Answer")8 }9 whenever(mock.method()).thenAnswer(answer)10 whenever(mock2.method()).thenAnswer(answer)11 whenever(mock3.method()).thenAnswer(answer)12 whenever(mock4.method()).thenAnswer(answer)13 runBlocking {14 mock.method()15 mock2.method()16 mock3.method()17 mock4.method()18 }19 }20 interface MyInterface {21 suspend fun method()22 }
SuspendableAnswer
Using AI Code Generation
1 val answer = SuspendableAnswer<String> { invocation ->2 "Hello " + invocation.getArgument<String>(0)3 }4 val mock = mock<SomeInterface> {5 onBlocking { doSomethingBlocking("World") } doAnswer answer6 }7 assertEquals("Hello World", mock.doSomethingBlocking("World"))8 }9}10fun <T> on(methodCall: T): OngoingStubbing<T> = on(methodCall) { }11internal class SuspendableAnswer<out R>(private val answer: suspend (InvocationOnMock)
SuspendableAnswer
Using AI Code Generation
1val mock = mock<MockedClass>()2val answer = SuspendableAnswer { invocation: InvocationOnMock ->3}4whenever(mock.function()).thenAnswer(answer)5val mock = mock<MockedClass>()6val answer = SuspendableAnswer { invocation: InvocationOnMock ->7}8whenever(moc
SuspendableAnswer
Using AI Code Generation
1val answer = SuspendableAnswer { invocation ->2 val suspendable = invocation.getArgument<Suspendable>(0)3 suspendable.suspendableMethod()4}5val mock = mock<SomeClass> {6 on { someMethod(any()) } doAnswer answer7}8verify(mock).someMethod(any())9verify(suspendable).suspendableMethod()10class SomeClass {11 suspend fun suspendableMethod() {12 }13}14mockkObject(SomeClass::class)15every { SomeClass::suspendableMethod } answers {16}17verify { SomeClass::suspendableMethod }18class SomeClass {19 suspend fun suspendableMethod(suspendable: Suspendable) {20 }21}22mockkStatic(SomeClass::class)23every { SomeClass::suspendableMethod(any()) } answers {24}25verify { SomeClass::suspendableMethod(any()) }26class SomeClass {27 suspend fun suspendableMethod(suspendable: Suspendable) {28 }29}30mockkStatic(SomeClass::class)31every { SomeClass::suspend
SuspendableAnswer
Using AI Code Generation
1 val mock = mock<SampleSuspendableService> {2 onBlocking { suspendableFunction() } doReturn SuspendableAnswer { "Hello" }3 }4 assertEquals("Hello", mock.suspendableFunction())5 }6 fun `test suspendable function with answer`() {7 val mock = mock<SampleSuspendableService> {8 onBlocking { suspendableFunction() } doAnswer SuspendableAnswer { "Hello" }9 }10 assertEquals("Hello", mock.suspendableFunction())11 }12 fun `test suspendable function with answer with param`() {13 val mock = mock<SampleSuspendableService> {14 onBlocking { suspendableFunctionWithParam(any()) } doAnswer SuspendableAnswer { "Hello" }15 }16 assertEquals("Hello", mock.suspendableFunctionWithParam("Hello"))17 }18 fun `test suspendable function with answer with param and return type`() {19 val mock = mock<SampleSuspendableService> {20 onBlocking { suspendableFunctionWithParamAndReturnType(any()) } doAnswer SuspendableAnswer { "Hello" }21 }22 assertEquals("Hello", mock.suspendableFunctionWithParamAndReturnType("Hello"))23 }24 fun `test suspendable function with answer with param and return type and suspend`() {25 val mock = mock<SampleSuspendableService> {26 onBlocking { suspendableFunctionWithParamAndReturnTypeAndSuspend(any()) } doAnswer SuspendableAnswer { "Hello" }27 }28 assertEquals("Hello", mock.suspendableFunctionWithParamAndReturnTypeAndSuspend("Hello"))29 }30}31class SampleSuspendableService {32 suspend fun suspendableFunction() = "Hello"33 suspend fun suspendableFunctionWithParam(param: String) = "Hello"
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!