Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.
See original GitHub issueModule 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_
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (3 by maintainers)
Top Results From Across the Web
org.koin.android.error.MissingAndroidContextException
MissingAndroidContextException: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration. – ...
Read more >Start Koin on Android
From your Application class. From your Application class you can use the startKoin function and inject the Android context with androidContext as follow:....
Read more >A brand new website interface for an even better experience!
Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.
Read more >Dependency Injection using Koin - Medium
Koin is the dependency injection framework purely built in Kotlin. ... Use given Android context; by inject() : allows to retrieve instances ......
Read more >I m trying to update to 2 0 0 beta 4 from 0 9 1 i WOULD have | Koin ...
applicationContext() I get a crash: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @pollux- If you have modules that require a
Context
instance, you need to provide it withandroidContex()
inside yourstartKoin
block.So ideally you need to change your code to:
I had a similar problem and was able to work around using MockK.
The module in question was creating shared prefs:
In order for my
checkModules
test to pass, I had to both mock the application and the prefs it returned: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.