Skip to content

Commit 50a1b91

Browse files
authored
Merge pull request #108 from nhaarman/release-0.10.1
Release 0.10.1
2 parents 3b5c40a + c3f7e68 commit 50a1b91

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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()

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,24 @@ inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
9393

9494
class 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

99116
infix fun <T> OngoingStubbing<T>.doReturn(t: T): OngoingStubbing<T> = thenReturn(t)

mockito-kotlin/src/test/kotlin/Classes.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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

6064
class ThrowableClass(cause: Throwable) : Throwable(cause)

mockito-kotlin/src/test/kotlin/MockitoTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

mockito-kotlin/src/testInlineMockito/kotlin/CreateInstanceOfImmutableTest.kt renamed to mockito-kotlin/src/testInlineMockito/kotlin/CreateInstanceInlineTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.junit.Test
2828
import java.io.IOException
2929
import 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)

0 commit comments

Comments
 (0)