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 {
6767 return MockitoKotlin .instanceCreator(kClass)?.invoke() as T ? ? :
6868 when {
6969 kClass.hasObjectInstance() -> kClass.objectInstance!!
70- kClass.isMockable() -> kClass.java.uncheckedMock()
7170 kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
71+ kClass.isMockable() -> kClass.java.uncheckedMock()
7272 kClass.isEnum() -> kClass.java.enumConstants.first()
7373 kClass.isArray() -> kClass.toArrayInstance()
7474 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 {
9393
9494class KStubbing <out T >(private val mock : T ) {
9595 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+ }
97114}
98115
99116infix 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 {
5454 fun nullableString (s : String? )
5555
5656 fun stringResult (): String
57- fun builderMethod () : Methods
57+ fun builderMethod (): Methods
58+ }
59+
60+ interface GenericMethods <T > {
61+ fun genericMethod (): T
5862}
5963
6064class ThrowableClass (cause : Throwable ) : Throwable(cause)
Original file line number Diff line number Diff line change @@ -436,4 +436,15 @@ class MockitoTest {
436436 expect(mock.stringResult()).toBe(" a" )
437437 expect(mock.stringResult()).toBe(" b" )
438438 }
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+ }
439450}
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ import org.junit.Test
2828import java.io.IOException
2929import java.math.BigInteger
3030
31- class CreateInstanceOfImmutableTest {
31+ class CreateInstanceInlineTest {
3232
3333 class ClassToBeMocked {
3434
@@ -97,6 +97,24 @@ class CreateInstanceOfImmutableTest {
9797 }
9898 }
9999
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+
100118 interface Methods {
101119
102120 fun throwableClass (t : ThrowableClass )
You can’t perform that action at this time.
0 commit comments