whenever
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 10This 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
method call to be stubbed.
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 10This 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
(regular or suspendable) lambda, wrapping the method/function call to be stubbed.
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
the mock to stub the method/function call on.
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
the mock to stub the method/function call on.
(regular or suspendable) lambda, wrapping the method/function call to be stubbed.