You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance and document stubbing API (apply infix and reified generics + apply whenever() with lambda arg as default in test cases). Also, deprecated methods onBlocking(methodCall), onGeneric(methodCall), wheneverBlocking(methodCall) to unify towards whenever(methodCall) and on(methodCall) as universal methods to stub any method call, being either synchronous, suspendable or with generics return type.
fun <R:Any> onGeneric(methodCall:T.() ->R?, c:KClass<R>): OngoingStubbing<R> {
55
-
val r =try {
56
-
mock.methodCall()
94
+
/**
95
+
* Enables stubbing java methods or kotlin functions. Use it when you want the mock to return
96
+
* a particular value when particular method/function is being called.
97
+
* The kotlin function call to be stubbed can be either a synchronous or suspend function.
98
+
*
99
+
* Simply put: "**on a call to** the x function **then** return y".
100
+
*
101
+
* Examples:
102
+
*
103
+
* ```kotlin
104
+
* stubbing(mock) {
105
+
* on { mock.someFunction() } doReturn 10
106
+
* }
107
+
* ```
108
+
*
109
+
* This function acts as an alias for [Mockito.when], as `when` is a keyword in kotlin and as such
110
+
* the Mockito method can only be called by wrapping the method name in backticks, e.g. `` `when` ``.
111
+
* To reduce the noise of backticks in your code, you can use this the function [whenever] instead.
112
+
*
113
+
* For more detail documentation, please refer to the Javadoc in the [Mockito] class.
114
+
*
115
+
* For stubbing Unit functions (or Java void methods) with throwables, see: [Mockito.doThrow].
116
+
*
117
+
* @param methodCall method call to be stubbed.
118
+
* @return OngoingStubbing object used to stub fluently.
119
+
* ***Do not*** create a reference to this returned object.
120
+
*/
121
+
fun <R> on(methodCall:suspendT.() ->R): OngoingStubbing<R> {
122
+
returntry {
123
+
whenever { mock.methodCall() }
57
124
} catch (e:NullPointerException) {
125
+
throwMockitoKotlinException(
126
+
"NullPointerException thrown when stubbing.\nThis may be due to two reasons:\n\t- The method you're trying to stub threw an NPE: look at the stack trace below;\n\t- You're trying to stub a generic method: try `onGeneric` instead.",
127
+
e
128
+
)
129
+
}
130
+
}
131
+
132
+
/**
133
+
* Enables stubbing java methods or kotlin functions with a generics return type [R].
134
+
* Use it when you want the mock to return a particular value when particular method/function
135
+
* is being called.
136
+
* The kotlin function call to be stubbed can be either a synchronous or suspend function.
137
+
*
138
+
* Simply put: "**on a call to** the x function **then** return y".
fun <R> on(methodCall:T.() ->R): OngoingStubbing<R> {
73
-
returntry {
74
-
Mockito.`when`(mock.methodCall())
75
-
} catch (e:NullPointerException) {
76
-
throwMockitoKotlinException(
77
-
"NullPointerException thrown when stubbing.\nThis may be due to two reasons:\n\t- The method you're trying to stub threw an NPE: look at the stack trace below;\n\t- You're trying to stub a generic method: try `onGeneric` instead.",
78
-
e
79
-
)
80
-
}
81
-
}
82
-
83
-
fun <T:Any, R> KStubbing<T>.onBlocking(
84
-
m:suspendT.() ->R
85
-
): OngoingStubbing<R> {
86
-
return runBlocking { Mockito.`when`(mock.m()) }
213
+
/**
214
+
* Enables stubbing a (suspendable) kotlin functions. Use it when you want the mock to return
215
+
* a particular value when particular function is being called.
216
+
* The kotlin function call to be stubbed can be either a synchronous or suspend function.
217
+
*
218
+
* Simply put: "**on a call to** the x function **then** return y".
219
+
*
220
+
* Examples:
221
+
*
222
+
* ```kotlin
223
+
* stubbing(mock) {
224
+
* onBlocking { mock.someFunction() } doReturn 10
225
+
* }
226
+
* ```
227
+
*
228
+
* This function acts as an alias for [Mockito.when], as `when` is a keyword in kotlin and as such
229
+
* the Mockito method can only be called by wrapping the method name in backticks, e.g. `` `when` ``.
230
+
* To reduce the noise of backticks in your code, you can use this function [on] instead.
231
+
*
232
+
* For more detail documentation, please refer to the Javadoc in the [Mockito] class.
233
+
*
234
+
* For stubbing Unit functions (or Java void methods) with throwables, see: [Mockito.doThrow].
235
+
*
236
+
* This is a deprecated alias for [on]. Please use [on] instead.
237
+
*
238
+
* @param methodCall (regular or suspendable) lambda, wrapping the function call to be stubbed.
239
+
* @return OngoingStubbing object used to stub fluently.
240
+
* ***Do not*** create a reference to this returned object.
241
+
*/
242
+
@Deprecated("Use on { methodCall } instead")
243
+
fun <T:Any, R> KStubbing<T>.onBlocking(methodCall:suspendT.() ->R): OngoingStubbing<R> {
0 commit comments