Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.koin.android.error.MissingAndroidContextException: when try to test app with context

Tags:

android

koin

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.

like image 254
Nurseyit Tursunkulov Avatar asked Dec 11 '22 02:12

Nurseyit Tursunkulov


1 Answers

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"
like image 168
jins joseph Avatar answered Feb 13 '23 04:02

jins joseph