Skip to content

Commit

Permalink
Merge pull request #108 from nhaarman/release-0.10.1
Browse files Browse the repository at this point in the history
Release 0.10.1
  • Loading branch information
nhaarman committed Nov 3, 2016
2 parents 3b5c40a + c3f7e68 commit 50a1b91
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
return MockitoKotlin.instanceCreator(kClass)?.invoke() as T? ?:
when {
kClass.hasObjectInstance() -> kClass.objectInstance!!
kClass.isMockable() -> kClass.java.uncheckedMock()
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
kClass.isMockable() -> kClass.java.uncheckedMock()
kClass.isEnum() -> kClass.java.enumConstants.first()
kClass.isArray() -> kClass.toArrayInstance()
kClass.isClassObject() -> kClass.toClassObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,24 @@ inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {

class KStubbing<out T>(private val mock: T) {
fun <R> on(methodCall: R) = Mockito.`when`(methodCall)
fun <R> on(methodCall: T.() -> R) = Mockito.`when`(mock.methodCall())

fun <R : Any> on(methodCall: T.() -> R, c: KClass<R>): OngoingStubbing<R> {
val r = try {
mock.methodCall()
} catch(e: NullPointerException) {
// An NPE may be thrown by the Kotlin type system when the MockMethodInterceptor returns a
// null value for a non-nullable generic type.
// We catch this NPE to return a valid instance.
// The Mockito state has already been modified at this point to reflect
// the wanted changes.
createInstance(c)
}
return Mockito.`when`(r)
}

inline fun <reified R : Any> on(noinline methodCall: T.() -> R): OngoingStubbing<R> {
return on(methodCall, R::class)
}
}

infix fun <T> OngoingStubbing<T>.doReturn(t: T): OngoingStubbing<T> = thenReturn(t)
Expand Down
6 changes: 5 additions & 1 deletion mockito-kotlin/src/test/kotlin/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ interface Methods {
fun nullableString(s: String?)

fun stringResult(): String
fun builderMethod() : Methods
fun builderMethod(): Methods
}

interface GenericMethods<T> {
fun genericMethod(): T
}

class ThrowableClass(cause: Throwable) : Throwable(cause)
11 changes: 11 additions & 0 deletions mockito-kotlin/src/test/kotlin/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,15 @@ class MockitoTest {
expect(mock.stringResult()).toBe("a")
expect(mock.stringResult()).toBe("b")
}

@Test
fun doReturn_withGenericIntReturnType() {
/* Given */
val mock = mock<GenericMethods<Int>> {
on { genericMethod() } doReturn 2
}

/* Then */
expect(mock.genericMethod()).toBe(2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.junit.Test
import java.io.IOException
import java.math.BigInteger

class CreateInstanceOfImmutableTest {
class CreateInstanceInlineTest {

class ClassToBeMocked {

Expand Down Expand Up @@ -97,6 +97,24 @@ class CreateInstanceOfImmutableTest {
}
}

@Test
fun createPrimitiveInstance() {
/* When */
val i = createInstance<Int>()

/* Then */
expect(i).toBe(0)
}

@Test
fun createStringInstance() {
/* When */
val s = createInstance<String>()

/* Then */
expect(s).toBe("")
}

interface Methods {

fun throwableClass(t: ThrowableClass)
Expand Down

0 comments on commit 50a1b91

Please sign in to comment.