Seeing an inconsistent issue with initialising Koi...
# koin
j
Seeing an inconsistent issue with initialising Koin when running iosX64 tests on a multiplatform project. I see get the following error when I run the entire suite of tests
kotlin.IllegalStateException: KoinApplication has not been started
. When I run the test individual the test passes as expected. For context each of my test classes extends implements this abstract class:
Copy code
abstract class KoinTest : KoinComponent {

    @BeforeTest
    fun beforeTest() {
        println("Starting Koin")
        startKoin {
            printLogger(Level.ERROR)
            modules(
                module {
                    single { Log(testing = useMocks) }
                },
            )
        }
    }

    @AfterTest
    fun after() {
        stopKoin()
    }
}
From my build.gradle
Copy code
dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
        classpath("com.android.tools.build:gradle:7.0.4")
}
Copy code
val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation("io.kotest:kotest-assertions-core:5.0.1")
                implementation("io.kotest:kotest-framework-engine:5.0.1")
                implementation("app.cash.turbine:turbine:0.7.0")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
                implementation("io.insert-koin:koin-core:3.1.5")
            }
        }
Has anyone come across any similar issues?
Found the issue. If you use:
Copy code
// is lazy evaluated
val myService : MyService by inject()
and then attempt to use an instance injected by Koin in the initialiser of that class it hasn't yet been injected by Koin. This effects iOS only. Instead use:
Copy code
// retrieve directly the instance
val myService : MyService = get()
This retrieves the dependency eager meaning it can be used immediately
a
cool, good catch 👍
379 Views