File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed
main/kotlin/com/nhaarman/mockito_kotlin Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -94,7 +94,7 @@ inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
94
94
class KStubbing <out T >(private val mock : T ) {
95
95
fun <R > on (methodCall : R ) = Mockito .`when `(methodCall)
96
96
97
- fun <R : Any > on (methodCall : T .() -> R , c : KClass <R >): OngoingStubbing <R > {
97
+ fun <R : Any > onGeneric (methodCall : T .() -> R , c : KClass <R >): OngoingStubbing <R > {
98
98
val r = try {
99
99
mock.methodCall()
100
100
} catch (e: NullPointerException ) {
@@ -108,8 +108,16 @@ class KStubbing<out T>(private val mock: T) {
108
108
return Mockito .`when `(r)
109
109
}
110
110
111
- inline fun <reified R : Any > on (noinline methodCall : T .() -> R ): OngoingStubbing <R > {
112
- return on(methodCall, R ::class )
111
+ inline fun <reified R : Any > onGeneric (noinline methodCall : T .() -> R ): OngoingStubbing <R > {
112
+ return onGeneric(methodCall, R ::class )
113
+ }
114
+
115
+ fun <R > on (methodCall : T .() -> R ): OngoingStubbing <R > {
116
+ return try {
117
+ Mockito .`when `(mock.methodCall())
118
+ } catch (e: NullPointerException ) {
119
+ throw MockitoKotlinException (" NullPointerException thrown when stubbing. If you are trying to stub a generic method, try `onGeneric` instead." , e)
120
+ }
113
121
}
114
122
}
115
123
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ interface Methods {
54
54
fun nullableString (s : String? )
55
55
56
56
fun stringResult (): String
57
+ fun nullableStringResult (): String?
57
58
fun builderMethod (): Methods
58
59
}
59
60
Original file line number Diff line number Diff line change @@ -351,6 +351,20 @@ class MockitoTest {
351
351
expect(result).toBeTheSameAs(mock)
352
352
}
353
353
354
+ @Test
355
+ fun testMockStubbing_nullable () {
356
+ /* Given */
357
+ val mock = mock<Methods > {
358
+ on { nullableStringResult() } doReturn " Test"
359
+ }
360
+
361
+ /* When */
362
+ val result = mock.nullableStringResult()
363
+
364
+ /* Then */
365
+ expect(result).toBe(" Test" )
366
+ }
367
+
354
368
@Test
355
369
fun testMockStubbing_doThrow () {
356
370
/* Given */
@@ -438,10 +452,22 @@ class MockitoTest {
438
452
}
439
453
440
454
@Test
441
- fun doReturn_withGenericIntReturnType () {
455
+ fun doReturn_withGenericIntReturnType_on () {
456
+ /* Expect */
457
+ expectErrorWithMessage(" onGeneric" ) on {
458
+
459
+ /* When */
460
+ mock<GenericMethods <Int >> {
461
+ on { genericMethod() } doReturn 2
462
+ }
463
+ }
464
+ }
465
+
466
+ @Test
467
+ fun doReturn_withGenericIntReturnType_onGeneric () {
442
468
/* Given */
443
469
val mock = mock<GenericMethods <Int >> {
444
- on { genericMethod() } doReturn 2
470
+ onGeneric { genericMethod() } doReturn 2
445
471
}
446
472
447
473
/* Then */
You can’t perform that action at this time.
0 commit comments