PowerOfCreation / KoinJUnit5ParallelExecution

1 stars 0 forks source link

The problem with "A Koin Application has already been started" is solved #1

Open jwattik opened 7 months ago

jwattik commented 7 months ago

The solution is quite simple, you need to create a singleton containing a koin instance and in the test base class simply override the getKoin method, which will receive the instance from the singleton.

Singleton with koin instance

object MyIsolatedKoinContext {

    val koinApp = koinApplication {
        // declare used modules
        modules(koinModule)
    }

    val koin = koinApp.koin
}

Test base class

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
abstract class BaseTest : KoinTest {

    override fun getKoin(): Koin = MyIsolatedKoinContext.koin
}
PowerOfCreation commented 7 months ago

That's a much better solution than what I came up with, thank you!