Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.10.1 #108

Merged
merged 4 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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