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
inline fun <T : Any> T.stub(stubbing: KStubbing<T>.(T) -> Unit): T {
44
48
return apply { KStubbing(this).stubbing(this) }
45
49
}
@@ -49,44 +53,85 @@ class KStubbing<out T : Any>(val mock: T) {
49
53
if (!mockingDetails(mock).isMock) throw NotAMockException("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
+
return try {
70
+
mockitoWhen(mock.methodCall())
71
+
} catch (e: NullPointerException) {
72
+
throw MockitoKotlinException(
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: suspend T.() -> R): OngoingStubbing<R> {
// An NPE may be thrown by the Kotlin type system when the MockMethodInterceptor returns a
59
108
// null value for a non-nullable generic type.
60
109
// We catch this NPE to return a valid instance.
61
110
// The Mockito state has already been modified at this point to reflect
62
111
// the wanted changes.
63
112
createInstance(c)
64
113
}
65
-
return Mockito.`when`(r)
114
+
return mockitoWhen(r)
66
115
}
67
116
68
-
inline fun <reified R : Any> onGeneric(noinline methodCall: T.() -> R?): OngoingStubbing<R> {
69
-
return onGeneric(methodCall, R::class)
117
+
/**
118
+
* 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
-
return try {
74
-
Mockito.`when`(mock.methodCall())
75
-
} catch (e: NullPointerException) {
76
-
throw MockitoKotlinException(
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
-
}
127
+
private fun <R> mockitoWhen(methodCall: R): OngoingStubbing<R> {
128
+
val ongoingStubbing = Mockito.`when`(methodCall)
82
129
83
-
fun <T : Any, R> KStubbing<T>.onBlocking(
84
-
m: suspend T.() -> R
85
-
): OngoingStubbing<R> {
86
-
return runBlocking { Mockito.`when`(mock.m()) }
87
-
}
130
+
if (ongoingStubbing.getMock<T>() != mock) {
131
+
mockingProgress().reset()
132
+
throw IllegalArgumentException("Stubbing of another mock is not allowed")
0 commit comments