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_
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() }
}
@cortinico how can I access context from my test class.
androidContext(androidContext: Context). it takes the context as an argument, how do I get that?
If you're running an Espresso test:
InstrumentationRegistry.getInstrumentation().getTargetContext()
If you're running a Roboelectric test:
RuntimeEnvironment.application
Sorry, I could have mentioned this before, I'm using Junit. How to inject context in that case
If you are using Mockito, do this:
@Mock private lateinit var context: Application
@Test
fun foo() {
startKoin {
androidContext(context)
modules(modules)
}
}
What's going wrong here !!
@RunWith(PowerMockRunner::class)
@PrepareForTest(AccountUseCaseImpl::class)
class AccountUseCaseImplTest : AutoCloseKoinTest() {
private val underTest: AccountUseCases by inject()
private val headerMapper: AccountHeaderMapper = mock()
private val accountRepository: AccountRepository = mock()
private val authenticationRepository: AuthenticationRepository = mock()
private val forgotPasswordRepository: ForgotPasswordRepository = mock()
private val sessionManager: SessionManager = mock()
private val networkUtil: NetworkUtil = mock()
private val gson: Gson = mock()
private val preferenceHelper: PreferenceHelper = mock()
private val appKeystore: AppKeystore = mock()
private val profileRepository: ProfileRepository = mock()
private val profileHeaderMapper: ProfileHeaderMapper = mock()
private val userManager: UserManager = mock()
private val sendOtpStatusResponseMock: SendOtpStatusResponse = mock()
private val intOauthResponseMock: InitOauthResponse = mock()
private val accountOauthResponseMock: AccountOauthResponse = mock()
private val statusMock: Status = mock()
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
startKoin {
printLogger(Level.DEBUG)
loadKoinModules(
listOf(module {
networkingModule
accountModule
authenticationModule
configurationModule
appModule
dashboardModule
oprModule
profileModule
})
)
}
}
val accountModule = module {
viewModel { LoginViewModel(get(), androidContext(), get()) }
viewModel { ConfirmationCodeViewModel(get(), get()) }
viewModel { VerifyAccountOptionsViewModel(get(), androidContext()) }
viewModel { ForgotPasswordViewModel(get(), androidContext()) }
viewModel { ResetPasswordViewModel(get(), androidContext()) }
viewModel { CreateAccountViewModel(get(), androidContext()) }
viewModel { CareMenuViewModel(get()) }
single { provideAccountRepository(get(named("redirectOn"))) }
single { provideAccountUseCase(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
single { provideForgotPasswordRepository(get(named("redirectOn"))) }
single { SessionManager() }
single { provideAccountRequestMapper(get()) }
single { provideUserManager() }
}
fun provideAccountUseCase(
headerMapper: AccountHeaderMapper,
accountRepository: AccountRepository,
authenticationRepository: AuthenticationRepository,
forgotPasswordRepository: ForgotPasswordRepository,
sessionManager: SessionManager,
networkUtil: NetworkUtil,
gson: Gson,
preferenceHelper: PreferenceHelper,
appKeystore: AppKeystore,
profileRepository: ProfileRepository,
profileHeaderMapper: ProfileHeaderMapper,
userManager: UserManager
): AccountUseCases {
return AccountUseCaseImpl(
headerMapper,
accountRepository,
authenticationRepository,
forgotPasswordRepository,
sessionManager,
networkUtil,
gson,
preferenceHelper,
appKeystore,
profileRepository,
profileHeaderMapper,
userManager
)
}
fun provideAccountRepository(apiRedirectOn: MhomServiceApi): AccountRepository {
return AccountRepositoryImpl(apiRedirectOn)
}
fun provideForgotPasswordRepository(apiRedirectOn: MhomServiceApi): ForgotPasswordRepository {
return ForgotPasswordRepositoryImpl(apiRedirectOn)
}
fun provideAccountRequestMapper(sessionManager: SessionManager): AccountHeaderMapper {
return AccountHeaderMapper(sessionManager)
}
fun provideUserManager(): UserManager {
return UserManager()
}
Getting this exception.
INFO] [Koin] load modules in 0.050028 ms
[DEBUG] [Koin] instances started in 0.017586 ms
[INFO] [Koin] closing scope:'-Root-'
[INFO] [Koin] closing scope:'-Root-'
[DEBUG] [Koin] +- get 'com.hom.domain.account.AccountUseCaseImpl'
[INFO] [Koin] closing scope:'-Root-'
[INFO] [Koin] stoppedorg.koin.core.error.NoBeanDefFoundException: No definition found for 'com.hom.domain.domain.account.AccountUseCaseImpl' has been found. Check your module definitions.
at org.koin.core.scope.Scope.findDefinition(Scope.kt:170)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:164)
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 com.hom.domain.account.AccountUseCaseImplTest$$special$$inlined$inject$1.invoke(KoinTest.kt:51)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.hom.domain.account.AccountUseCaseImplTest.getUnderTest(AccountUseCaseImplTest.kt)
at com.hom.domain.account.AccountUseCaseImplTest.request_send_otp_success(AccountUseCaseImplTest.kt:98)
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.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
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)
You just need to mock a Application with Mockito and declare on androidContext() as I've commented:
@Mock private lateinit var context: Application
But you are getting another error:
org.koin.core.error.NoBeanDefFoundException: No definition found for 'com.hom.domain.domain.account.AccountUseCaseImpl' has been found. Check your module definitions.
Something is wrong with your modules.
The same module works perfectly when using the app, but not in the test
Try replacing for this:
single { provideAccountUseCase(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) as AccountUseCaseImpl }
Sure will try, thanks
Any updates, @pollux- ?
That didn't work either
You are reffering directly somewhere with AccountUseCaseImpl and not its interface
```
class App : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// declare used Android context
androidContext(this@App)
modules(viewModelModule)
}
}
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.
Most helpful comment
Hey @pollux-
If you have modules that require a
Contextinstance, you need to provide it withandroidContex()inside yourstartKoinblock.So ideally you need to change your code to: