File tree Expand file tree Collapse file tree 5 files changed +54
-4
lines changed
main/kotlin/com/nhaarman/mockito_kotlin Expand file tree Collapse file tree 5 files changed +54
-4
lines changed Original file line number Diff line number Diff line change @@ -67,8 +67,8 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
67
67
return MockitoKotlin .instanceCreator(kClass)?.invoke() as T ? ? :
68
68
when {
69
69
kClass.hasObjectInstance() -> kClass.objectInstance!!
70
- kClass.isMockable() -> kClass.java.uncheckedMock()
71
70
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
71
+ kClass.isMockable() -> kClass.java.uncheckedMock()
72
72
kClass.isEnum() -> kClass.java.enumConstants.first()
73
73
kClass.isArray() -> kClass.toArrayInstance()
74
74
kClass.isClassObject() -> kClass.toClassObject()
Original file line number Diff line number Diff line change @@ -93,7 +93,24 @@ inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
93
93
94
94
class KStubbing <out T >(private val mock : T ) {
95
95
fun <R > on (methodCall : R ) = Mockito .`when `(methodCall)
96
- fun <R > on (methodCall : T .() -> R ) = Mockito .`when `(mock.methodCall())
96
+
97
+ fun <R : Any > on (methodCall : T .() -> R , c : KClass <R >): OngoingStubbing <R > {
98
+ val r = try {
99
+ mock.methodCall()
100
+ } catch (e: NullPointerException ) {
101
+ // An NPE may be thrown by the Kotlin type system when the MockMethodInterceptor returns a
102
+ // null value for a non-nullable generic type.
103
+ // We catch this NPE to return a valid instance.
104
+ // The Mockito state has already been modified at this point to reflect
105
+ // the wanted changes.
106
+ createInstance(c)
107
+ }
108
+ return Mockito .`when `(r)
109
+ }
110
+
111
+ inline fun <reified R : Any > on (noinline methodCall : T .() -> R ): OngoingStubbing <R > {
112
+ return on(methodCall, R ::class )
113
+ }
97
114
}
98
115
99
116
infix fun <T > OngoingStubbing<T>.doReturn (t : T ): OngoingStubbing <T > = thenReturn(t)
Original file line number Diff line number Diff line change @@ -54,7 +54,11 @@ interface Methods {
54
54
fun nullableString (s : String? )
55
55
56
56
fun stringResult (): String
57
- fun builderMethod () : Methods
57
+ fun builderMethod (): Methods
58
+ }
59
+
60
+ interface GenericMethods <T > {
61
+ fun genericMethod (): T
58
62
}
59
63
60
64
class ThrowableClass (cause : Throwable ) : Throwable(cause)
Original file line number Diff line number Diff line change @@ -436,4 +436,15 @@ class MockitoTest {
436
436
expect(mock.stringResult()).toBe(" a" )
437
437
expect(mock.stringResult()).toBe(" b" )
438
438
}
439
+
440
+ @Test
441
+ fun doReturn_withGenericIntReturnType () {
442
+ /* Given */
443
+ val mock = mock<GenericMethods <Int >> {
444
+ on { genericMethod() } doReturn 2
445
+ }
446
+
447
+ /* Then */
448
+ expect(mock.genericMethod()).toBe(2 )
449
+ }
439
450
}
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ import org.junit.Test
28
28
import java.io.IOException
29
29
import java.math.BigInteger
30
30
31
- class CreateInstanceOfImmutableTest {
31
+ class CreateInstanceInlineTest {
32
32
33
33
class ClassToBeMocked {
34
34
@@ -97,6 +97,24 @@ class CreateInstanceOfImmutableTest {
97
97
}
98
98
}
99
99
100
+ @Test
101
+ fun createPrimitiveInstance () {
102
+ /* When */
103
+ val i = createInstance<Int >()
104
+
105
+ /* Then */
106
+ expect(i).toBe(0 )
107
+ }
108
+
109
+ @Test
110
+ fun createStringInstance () {
111
+ /* When */
112
+ val s = createInstance<String >()
113
+
114
+ /* Then */
115
+ expect(s).toBe(" " )
116
+ }
117
+
100
118
interface Methods {
101
119
102
120
fun throwableClass (t : ThrowableClass )
You can’t perform that action at this time.
0 commit comments