File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
main/kotlin/com/nhaarman/mockito_kotlin Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,17 @@ inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(
79
79
inline fun <reified T : Any > mock (s : MockSettings ): T = Mockito .mock(T ::class .java, s)!!
80
80
inline fun <reified T : Any > mock (s : String ): T = Mockito .mock(T ::class .java, s)!!
81
81
82
+ inline fun <reified T : Any > mock (stubbing : KStubbing <T >.() -> Unit ): T
83
+ = Mockito .mock(T ::class .java)!! .apply { stubbing(KStubbing (this )) }
84
+
85
+ class KStubbing <out T >(private val mock : T ) {
86
+ fun <R > on (methodCall : R ) = Mockito .`when `(methodCall)
87
+ fun <R > on (methodCall : T .() -> R ) = Mockito .`when `(mock.methodCall())
88
+ }
89
+
90
+ fun <T > OngoingStubbing<T>.doReturn (t : T ) = thenReturn(t)
91
+ fun <T > OngoingStubbing<T>.doReturn (t : T , vararg ts : T ) = thenReturn(t, * ts)
92
+
82
93
fun mockingDetails (toInspect : Any ): MockingDetails = Mockito .mockingDetails(toInspect)!!
83
94
fun never (): VerificationMode = Mockito .never()!!
84
95
inline fun <reified T : Any > notNull (): T ? = Mockito .notNull(T ::class .java)
Original file line number Diff line number Diff line change @@ -282,4 +282,48 @@ class MockitoTest {
282
282
mock.go()
283
283
}
284
284
}
285
+
286
+ @Test
287
+ fun testMockStubbing_lambda () {
288
+ /* Given */
289
+ val mock = mock<Open >() {
290
+ on { stringResult() }.doReturn(" A" )
291
+ }
292
+
293
+ /* When */
294
+ val result = mock.stringResult()
295
+
296
+ /* Then */
297
+ expect(result).toBe(" A" )
298
+ }
299
+
300
+ @Test
301
+ fun testMockStubbing_normalOverridesLambda () {
302
+ /* Given */
303
+ val mock = mock<Open >() {
304
+ on { stringResult() }.doReturn(" A" )
305
+ }
306
+ whenever(mock.stringResult()).thenReturn(" B" )
307
+
308
+ /* When */
309
+ val result = mock.stringResult()
310
+
311
+ /* Then */
312
+ expect(result).toBe(" B" )
313
+ }
314
+
315
+ @Test
316
+ fun testMockStubbing_methodCall () {
317
+ /* Given */
318
+ val mock = mock<Open >()
319
+ mock<Open > {
320
+ on(mock.stringResult()).doReturn(" A" )
321
+ }
322
+
323
+ /* When */
324
+ val result = mock.stringResult()
325
+
326
+ /* Then */
327
+ expect(result).toBe(" A" )
328
+ }
285
329
}
You can’t perform that action at this time.
0 commit comments