com.nhaarman.mockito_kotlin.createinstance.NonNullProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-kotlin Show documentation
Show all versions of mockito-kotlin Show documentation
Using Mockito with Kotlin.
package com.nhaarman.mockito_kotlin.createinstance
import kotlin.reflect.KClass
import java.lang.reflect.Array as JavaArray
interface NonNullProvider {
fun createInstance(kClass: KClass): T
}
fun nonNullProvider(): NonNullProvider = NonNullProviderImpl(listOf(NullCaster(), InstanceCreator()))
internal class NonNullProviderImpl(
private val nonNullProviders: List
) : NonNullProvider {
override fun createInstance(kClass: KClass): T {
return firstNonErroring(
nonNullProviders.map { { it.createInstance(kClass) } }
)
}
private fun firstNonErroring(functions: List<() -> T>): T {
var error: Throwable? = null
functions.forEach { f ->
try {
return f()
} catch(e: Throwable) {
error = e
}
}
throw error ?: IllegalStateException()
}
}