我无法将 Koin 2.0.1 与 Kotlin-test 3.4.2 一起使用。我得到这样的 InvocationTargetException :
Running koinexample.KoinSampleTests
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.009 sec <<< FAILURE! - in koinexample.KoinSampleTests
koinexample.KoinSampleTests Time elapsed: 0.009 sec <<< ERROR!
java.lang.reflect.InvocationTargetException
at koinexample.KoinSampleTests.getKoin(KoinSampleTests.kt:26)
at koinexample.KoinSampleTests.<init>(KoinSampleTests.kt:61)
我在 GitHub 上创建了一个重现此错误的小示例: https ://github.com/elifarley/kotlin-tests-with-koin-examples
只需执行这些命令来克隆 repo 并运行测试:
git clone https://github.com/elifarley/kotlin-tests-with-koin-examples.git
cd kotlin-tests-with-koin-examples
mvn
这是主要的 Kotlin 文件:
package koinexample
import io.kotlintest.koin.KoinListener
import io.kotlintest.shouldBe
import io.kotlintest.specs.FreeSpec
import org.koin.core.inject
import org.koin.dsl.module
import org.koin.test.KoinTest
data class Stats(var ok: Long = 0, var error: Long = 0)
interface StatsServer {
fun newError(): Long
}
class StatsServerSimple(private val stats: Stats) : StatsServer {
override fun newError() = stats.error++
}
val appModule = module {
single { Stats() }
single { StatsServerSimple(get()) as StatsServer }
}
class KoinSampleTests : FreeSpec(), KoinTest {
private val modules = listOf(
appModule
)
override fun listeners() = listOf(KoinListener(modules))
val statsServer: StatsServer by inject()
init {
"Happy path" {
statsServer.newError() shouldBe 1
}
}
}