whenever

inline fun <T> LenientStubber.whenever(methodCall: T): OngoingStubbing<T>
inline fun <T> LenientStubber.whenever(methodCall: () -> T): OngoingStubbing<T>
fun <S, T> MockedStatic<T>.whenever(verification: () -> S): OngoingStubbing<S>


fun <T> whenever(methodCall: T): OngoingStubbing<T>

Enables stubbing methods/function calls. In case of Kotlin function calls, these can be either synchronous or suspendable function calls.

Simply put: "Whenever the x function is being called then return y".

Examples:

    whenever(mock.someFunction()) doReturn 10

//you can use flexible argument matchers, e.g:
whenever(mock.someFunction(any())) 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.


fun <T> whenever(methodCall: suspend () -> T): OngoingStubbing<T>

Enables stubbing methods/function calls. In case of Kotlin function calls, these can be either synchronous or suspendable function calls.

Warning: Only the first method/function call in the lambda will be stubbed, other methods/functions calls are ignored!

Simply put: "Whenever the x function is being called then return y".

Examples:

    whenever { mock.someFunction() } doReturn 10

//you can use flexible argument matchers, e.g:
whenever { mock.someFunction(any()) } 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

(regular or suspendable) lambda, wrapping the method/function call to be stubbed.


fun <T> Stubber.whenever(mock: T): T

Sets the mock to apply the reverse stubbing on.

Reverse stubbing is especially useful when stubbing a void method (or Unit function) to throw an exception.

Example:

    mock<SynchronousFunctions> {
doThrow(RuntimeException()).whenever { string("test") }
}

This function is an alias for Mockito's Mockito.when. So, for more detailed documentation, please refer to the Javadoc of that method in the Mockito class.

Return

the mock used to stub the method/function call fluently.

Parameters

mock

the mock to stub the method/function call on.


fun <T> Stubber.whenever(mock: T, methodCall: suspend T.() -> Unit)

Sets the mock and the method call to be stubbed. With this version of whenever you can reverse stub either synchronous or suspendable function calls.

Reverse stubbing is especially useful when stubbing a void method (or Unit function) to throw an exception.

Warning: Only one method call can be stubbed in the function. Subsequent method calls are ignored!

Example:

    doThrow(RuntimeException()).whenever(mock) { someMethod() }

This function is an alias for Mockito's Mockito.when. So, for more detailed documentation, please refer to the Javadoc of that method in the Mockito class.

Parameters

mock

the mock to stub the method/function call on.

methodCall

(regular or suspendable) lambda, wrapping the method/function call to be stubbed.