Skip to content

Commit

Permalink
Merge pull request #110 from nhaarman/graciously_fail
Browse files Browse the repository at this point in the history
Add backup when mocking fails
  • Loading branch information
nhaarman authored Nov 3, 2016
2 parents fa27978 + c8fecf0 commit 771f761
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 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
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()
}
}
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 771f761

Please sign in to comment.