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
* Apply stubbing on the given mock. The stubbed behavior of the mock can then be specified in a supplied lambda.
46
+
*/
43
47
inlinefun <T:Any> T.stub(stubbing:KStubbing<T>.(T) ->Unit): T {
44
48
returnapply { KStubbing(this).stubbing(this) }
45
49
}
@@ -49,44 +53,85 @@ class KStubbing<out T : Any>(val mock: T) {
49
53
if (!mockingDetails(mock).isMock) throwNotAMockException("Stubbing target is not a mock!")
50
54
}
51
55
52
-
fun <R> on(methodCall:R): OngoingStubbing<R> =Mockito.`when`(methodCall)
56
+
/**
57
+
* Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.
58
+
*
59
+
* Simply put: `When the x method is called then return y`
60
+
*/
61
+
fun <R> on(methodCall:R): OngoingStubbing<R> = mockitoWhen(methodCall)
62
+
63
+
/**
64
+
* Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.
65
+
*
66
+
* Simply put: `When the x method is called then return y`
67
+
*/
68
+
fun <R> on(methodCall:T.() ->R): OngoingStubbing<R> {
69
+
returntry {
70
+
mockitoWhen(mock.methodCall())
71
+
} catch (e:NullPointerException) {
72
+
throwMockitoKotlinException(
73
+
"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.",
74
+
e
75
+
)
76
+
}
77
+
}
78
+
79
+
/**
80
+
* Enables stubbing suspend functions. Use it when you want the mock to return particular value when particular suspend function is called.
81
+
*
82
+
* Simply put: `When the x suspend function is called then return y`
83
+
*/
84
+
fun <R> onBlocking(suspendFunctionCall:suspendT.() ->R): OngoingStubbing<R> {
* Completes stubbing a method, by addressing the method call to apply a given stubbed [org.mockito.stubbing.Answer] on.
119
+
* Use it when you want the mock to return particular value when particular method is called.
120
+
*
121
+
* Simply put: `Return y when the x method is called`
122
+
*/
123
+
fun Stubber.on(methodCall:T.() ->Unit) {
124
+
this.`when`(mock).methodCall()
70
125
}
71
126
72
-
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.",
0 commit comments