Pavlo Kravchenko
Pavlo Kravchenko

Reputation: 375

How fix No definition found for class:'android.content.Context'. Check your definitions! in KoinTest

In my KoinTest extends

@Before
    fun setUp() {
        startKoin { modules(KoinStarter.getModules()) }
    }

@Test
fun `should inject my components`() {
    val settingsStore: SettingsStore = get()
    assertNotNull(settingsStore)
}

I get error No definition found for class:'android.content.Context'. Check your definitions!

But my module located in KoinStarter.getModules()

val localDataModule = module {
    factory<Resources> { get<Context>().resources }
    single<SettingsStore> { SettingsStore(get<Context>()) }
}

Upvotes: 3

Views: 2758

Answers (1)

Feedbacker
Feedbacker

Reputation: 969

You need to provide the application context in the androidContext() method.

Since your question is about testing specifically, in my case I had to pass application context via ApplicationProvider:

startKoin {
    androidContext(ApplicationProvider.getApplicationContext())
    .....
}

Don't forget to add the androidx dependency:

testImplementation 'androidx.test:core:1.0.0'

Upvotes: 4

Related Questions