onGeneric

fun <R : Any> onGeneric(methodCall: suspend T.() -> R?, clazz: KClass<R>): OngoingStubbing<R>

Enables stubbing methods/function calls with a generics return type R. In case of Kotlin function calls, these can be either synchronous or suspendable function calls.

Simply put: "on a call to the x function then return y".

Examples:

    interface GenericMethods<T> {
fun genericMethod(): T
}

mock<GenericMethods<Int>> {
onGeneric({ genericMethod() }, Int::class) doReturn 10
}

This function is an alias for Mockito.when. So, for more detailed documentation, please refer to the Javadoc of that method in the Mockito class. For stubbing Unit functions (or Java void methods) with throwables, see: Mockito.doThrow.

Return

OngoingStubbing object used to stub fluently. Do not create a reference to this returned object.

Parameters

methodCall

method call to be stubbed.

clazz

the generics type.


inline fun <R : Any> onGeneric(noinline methodCall: suspend T.() -> R?): OngoingStubbing<R>

Deprecated

Use on { mock.methodCall() } instead

Enables stubbing methods/function calls with a generics return type R. In case of Kotlin function calls, these can be either synchronous or suspendable function calls.

Simply put: "on a call to the x function then return y".

Examples:

    interface GenericMethods<T> {
fun genericMethod(): T
}

mock<GenericMethods<Int>> {
onGeneric { genericMethod() } doReturn 10
}

This is a deprecated alias for on. Please use on instead.

Return

OngoingStubbing object used to stub fluently. Do not create a reference to this returned object.

Parameters

methodCall

method call to be stubbed.