Skip to content

Commit

Permalink
Merge pull request #111 from nhaarman/release-0.11.0
Browse files Browse the repository at this point in the history
Release 0.11.0
  • Loading branch information
nhaarman authored Nov 3, 2016
2 parents 50a1b91 + 771f761 commit 27c6bf6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,31 @@ inline fun <reified T : Any> createInstance() = createInstance(T::class)

@Suppress("UNCHECKED_CAST")
fun <T : Any> createInstance(kClass: KClass<T>): T {
var cause: Throwable? = null
return MockitoKotlin.instanceCreator(kClass)?.invoke() as T? ?:
when {
kClass.hasObjectInstance() -> kClass.objectInstance!!
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
kClass.isMockable() -> kClass.java.uncheckedMock()
kClass.isEnum() -> kClass.java.enumConstants.first()
kClass.isArray() -> kClass.toArrayInstance()
kClass.isClassObject() -> kClass.toClassObject()
else -> kClass.easiestConstructor().newInstance()
try {
when {
kClass.hasObjectInstance() -> kClass.objectInstance!!
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
kClass.isEnum() -> kClass.java.enumConstants.first()
kClass.isArray() -> kClass.toArrayInstance()
kClass.isClassObject() -> kClass.toClassObject()
kClass.isMockable() -> try {
kClass.java.uncheckedMock()
} catch(e: Throwable) {
cause = e
kClass.easiestConstructor().newInstance()
}
else -> kClass.easiestConstructor().newInstance()
}
} catch(e: Exception) {
if (e is MockitoKotlinException) throw e

cause?.let {
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
(e as java.lang.Throwable).initCause(it)
}
throw MockitoKotlinException("Could not create an instance for $kClass.", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ 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 : Any> on(methodCall: T.() -> R, c: KClass<R>): OngoingStubbing<R> {
fun <R : Any> onGeneric(methodCall: T.() -> R, c: KClass<R>): OngoingStubbing<R> {
val r = try {
mock.methodCall()
} catch(e: NullPointerException) {
Expand All @@ -108,8 +108,16 @@ class KStubbing<out T>(private val mock: T) {
return Mockito.`when`(r)
}

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

fun <R> on(methodCall: T.() -> R): OngoingStubbing<R> {
return try {
Mockito.`when`(mock.methodCall())
} catch(e: NullPointerException) {
throw MockitoKotlinException("NullPointerException thrown when stubbing. If you are trying to stub a generic method, try `onGeneric` instead.", e)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions mockito-kotlin/src/test/kotlin/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface Methods {
fun nullableString(s: String?)

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

Expand Down
22 changes: 22 additions & 0 deletions mockito-kotlin/src/test/kotlin/CreateInstanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@ class CreateInstanceTest {
expect(result.second).toBe(2)
}

@Test
fun sealedClass() {
/* When */
val result = createInstance(MySealedClass::class)

/* Then */
expect(result).toNotBeNull()
}

@Test
fun sealedClassMember() {
/* When */
val result = createInstance(MySealedClass.MySealedClassMember::class)

/* Then */
expect(result).toNotBeNull()
}

private class PrivateClass private constructor(val data: String)

class ClosedClass
Expand Down Expand Up @@ -537,4 +555,8 @@ class CreateInstanceTest {
}

enum class MyEnum { VALUE, ANOTHER_VALUE }

sealed class MySealedClass {
class MySealedClassMember : MySealedClass()
}
}
30 changes: 28 additions & 2 deletions mockito-kotlin/src/test/kotlin/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ class MockitoTest {
expect(result).toBeTheSameAs(mock)
}

@Test
fun testMockStubbing_nullable() {
/* Given */
val mock = mock<Methods> {
on { nullableStringResult() } doReturn "Test"
}

/* When */
val result = mock.nullableStringResult()

/* Then */
expect(result).toBe("Test")
}

@Test
fun testMockStubbing_doThrow() {
/* Given */
Expand Down Expand Up @@ -438,10 +452,22 @@ class MockitoTest {
}

@Test
fun doReturn_withGenericIntReturnType() {
fun doReturn_withGenericIntReturnType_on() {
/* Expect */
expectErrorWithMessage("onGeneric") on {

/* When */
mock<GenericMethods<Int>> {
on { genericMethod() } doReturn 2
}
}
}

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

/* Then */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.mockito_kotlin.*
import org.junit.Test
import java.io.IOException
Expand Down Expand Up @@ -105,7 +106,7 @@ class CreateInstanceInlineTest {
/* Then */
expect(i).toBe(0)
}

@Test
fun createStringInstance() {
/* When */
Expand All @@ -115,10 +116,33 @@ class CreateInstanceInlineTest {
expect(s).toBe("")
}

@Test
fun sealedClass_fails() {
/* Expect */
expectErrorWithMessage("Could not create") on {

/* When */
createInstance(MySealedClass::class)
}
}

@Test
fun sealedClassMember() {
/* When */
val result = createInstance(MySealedClass.MySealedClassMember::class)

/* Then */
expect(result).toNotBeNull()
}

interface Methods {

fun throwableClass(t: ThrowableClass)
}

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

sealed class MySealedClass {
class MySealedClassMember : MySealedClass()
}
}

0 comments on commit 27c6bf6

Please sign in to comment.