5

I want to write test for koin. I use RoomDatabase, which receives context in constructor. App works well but test fails

Can't resolve Application instance. Please use androidContext() function in your KoinApplication configuration.

Basi
  • 3,009
  • 23
  • 28
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78

6 Answers6

7

if you want context in your modules, you should pass context in your start koin method

// start Koin!
    startKoin {

      // declare used Android context
      androidContext(this@MyApplication)

      // declare modules
      modules(yourModule)
    }

and use these libraries

    // Koin AndroidX Scope features
    implementation "org.koin:koin-android-scope:2.0.1"
// Koin AndroidX ViewModel features
    implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
// Koin AndroidX Experimental features
    implementation "org.koin:koin-android-ext:2.0.1"
jins joseph
  • 271
  • 1
  • 11
4

Make sure to set your context when you start koin. Then you should be able to access it via androidContext() in your modules.

startKoin {
    androidContext(this@App)
    modules(...)
    ...
}
woodii
  • 763
  • 7
  • 23
2

For unittests I have implemented this

@file:JvmName("KoinTest")
package com.myapp.di

import androidx.test.core.app.ApplicationProvider
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

fun startKoin() {
    startKoin {
        androidContext(ApplicationProvider.getApplicationContext())
        modules(listOf(applicationModule, networkModule))
    }
}

fun stopKoin() {
    org.koin.core.context.stopKoin()
}

I can call this from our old Java tests as KoinTest.startKoin() or just startKoin() in Kotlin. I call these from the @Before and @After block appropriately. The android context is set with the new AndroidX test library function.

Simon Featherstone
  • 1,716
  • 23
  • 40
1

You cannot test Room in unit testing with Koin. You can only test in instrumented tests.

Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
1

in my case, I edit my application class to :

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        startKoin {

            androidContext(this@App)

            modules(viewModelModule)
        }
    }
}
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

KOIN : 3.2.0-beta-1

// Koin Core features
    implementation "io.insert-koin:koin-core:3.2.0-beta-1"
    implementation "io.insert-koin:koin-android:3.2.0-beta-1"

declare context under startkoin like below

androidContext(this@KoinSampleApplication)

class KoinSampleApplication : Application() {

override fun onCreate() {
    super.onCreate()
    startKoinDI()
}

private fun startKoinDI() {
    startKoin {

        // declare context
        androidContext(this@KoinSampleApplication)

        // declare modules
        modules(
            networkModule,
            repositoryModule,
            servicesModule,
            utilityModule,
            viewModelModule,
        )

    }
    }
}

Hope it will work.

Yogendra
  • 4,817
  • 1
  • 28
  • 21