InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.12k stars 721 forks source link

InvocationTargetException: Koin 2.0.1 with Kotlin-test 3.4.2 #717

Closed elifarley closed 4 years ago

elifarley commented 4 years ago

Describe the bug I'm unable to use Koin 2.0.1 with Kotlin-test 3.4.2 and Maven. I get an InvocationTargetException like this:

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)

To Reproduce I've created a small example on GitHub that reproduces this error: https://github.com/elifarley/kotlin-tests-with-koin-examples Just execute these commands to clone the repo and run tests:

git clone https://github.com/elifarley/kotlin-tests-with-koin-examples.git
cd kotlin-tests-with-koin-examples
mvn

Expected behavior The test should either pass or fail, but no InvocationTargetException should be thrown.

Koin project used and used version (please complete the following information): koin-core version 2.0.1

jeziellago commented 4 years ago

Hi @elifarley!

In your code (https://github.com/elifarley/kotlin-tests-with-koin-examples.git), val statsServer: StatsServer by inject() calls getKoin() internally, which not started yet. (Koin will be started after listeners method which contains the KoinListener).

Try write your test case using get instead by inject:

"Happy path" {
    get<StatsServer>().newError() shouldBe 1
}