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

Module configuration

val healthRecordModule = module {
    viewModel { HealthRecordViewModel(get()) }
    viewModel { HealthVisitViewModel(get(), get()) }
    viewModel { RecordsViewModel() }
    viewModel { LabResultsViewModel(get()) }
    viewModel { DateFilterViewModel(get()) }
    factory { DateFilterMapper(androidContext()) }
    factory { HealthRecordDataMapper(androidContext()) }
    factory { HealthVisitDataMapper(androidContext()) }
    factory { ViewAllResultsDataMapper(androidContext()) }
    factory { LabResultsDataMapper() }
}

package com.hcahealthcare.mhom.domain.account

import com.hcahealthcare.mhom.di.account.accountModule
import com.hcahealthcare.mhom.di.appModule
import com.hcahealthcare.mhom.di.authentication.authenticationModule
import com.hcahealthcare.mhom.di.configuration.configurationModule
import com.hcahealthcare.mhom.di.dashboardModule
import com.hcahealthcare.mhom.di.healthrecord.healthRecordModule
import com.hcahealthcare.mhom.di.networkingModule
import com.hcahealthcare.mhom.di.opr.oprModule
import com.hcahealthcare.mhom.di.profile.profileModule
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.koinApplication
import org.koin.test.AutoCloseKoinTest
import org.koin.test.check.checkModules

/**
 * Dry run configuration
 */
class CheckModulesTest : AutoCloseKoinTest() {

    @Test
    fun dryRunTest() {
        startKoin {
            printLogger(Level.DEBUG)
            modules(listOf(
                networkingModule, accountModule, authenticationModule, configurationModule,
                appModule, dashboardModule, oprModule, profileModule, healthRecordModule
            ))
        }.checkModules()
    }
}

this is failing with


_rg.koin.android.error.MissingAndroidContextException: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.

	at org.koin.android.ext.koin.ModuleExtKt.androidContext(ModuleExt.kt:33)
	at com.hcahealthcare.mhom.di.healthrecord.HealthRecordModuleKt$healthRecordModule$1$9.invoke(healthRecordModule.kt:18)
	at com.hcahealthcare.mhom.di.healthrecord.HealthRecordModuleKt$healthRecordModule$1$9.invoke(healthRecordModule.kt)
	at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54)
	at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37)
	at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)
	at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
	at org.koin.core.scope.Scope.access$resolveInstance(Scope.kt:33)
	at org.koin.core.scope.Scope$get$$inlined$synchronized$lambda$1.invoke(Scope.kt:123)
	at org.koin.core.time.MeasureKt.measureDuration(Measure.kt:37)
	at org.koin.core.scope.Scope.get(Scope.kt:122)
	at org.koin.core.Koin.get(Koin.kt:107)
	at org.koin.test.check.CheckModulesKt.checkMainDefinitions(CheckModules.kt:60)
	at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:38)
	at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:25)
	at org.koin.test.check.CheckModulesKt.checkModules$default(CheckModules.kt:25)
	at com.hcahealthcare.mhom.domain.account.CheckModulesTest.dryRunTest(CheckModulesTest.kt:32)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code 255_

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Hey @pollux- If you have modules that require a Context instance, you need to provide it with androidContex() inside your startKoin block.

So ideally you need to change your code to:

    fun dryRunTest() {
        startKoin {
            printLogger(Level.DEBUG)
            androidContext(...)
            modules(listOf(..., healthRecordModule))
        }.checkModules()
    }
val healthRecordModule = module {
    viewModel { HealthRecordViewModel(get()) }
    viewModel { HealthVisitViewModel(get(), get()) }
    viewModel { RecordsViewModel() }
    viewModel { LabResultsViewModel(get()) }
    viewModel { DateFilterViewModel(get()) }
    factory { DateFilterMapper(get()) }
    factory { HealthRecordDataMapper(get()) }
    factory { HealthVisitDataMapper(get()) }
    factory { ViewAllResultsDataMapper(get()) }
    factory { LabResultsDataMapper() }
}

I had a similar problem and was able to work around using MockK.

The module in question was creating shared prefs:

val coreModule = module {
    single {
        androidApplication().getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
    }
}

In order for my checkModules test to pass, I had to both mock the application and the prefs it returned:

class KoinModulesTest : KoinTest {
    private val mockApplication: Application = mockk()
    private val mockPreferences: SharedPreferences = mockk()

    @Test
    fun checkViewModelModules() {
        every {
            mockApplication.getSharedPreferences(any(), any())
        } returns mockPreferences

        koinApplication {
            androidContext(mockApplication)
            modules(appModules)
        }.checkModules()
    }
}

I’m not sure if this is a code smell, and there’s a better way to deal with SharedPreferences in Koin, but dropping this here to help the next person.