Karim Houari
03/11/2024, 11:32 AMFernando Sanchez (Perraco Labs)
03/11/2024, 4:59 PMKarim Houari
03/11/2024, 5:10 PMloadKoinModules
in my test to add the test implementation to be injected instead but I get KoinApplication has not been started
Karim Houari
03/11/2024, 5:11 PMinstall(Koin) {
modules(appModule)
}
Karim Houari
03/11/2024, 5:11 PMval appModule = module {
single { DateProvider { org.joda.time.LocalDate.now() } }
}
Fernando Sanchez (Perraco Labs)
03/11/2024, 5:19 PM@Test
fun yourTest() = testApplication {
// Start the application so that the Koin DI container is initialized.
startApplication()
.....
}
See this test in my pet project:
https://github.com/perracodex/Kcrud/blob/master/kcrud-server/src/test/kotlin/BackPressureTest.kt
If instead, you are testing a functionality that does not start your server module, then you need to start Koin manually in the actual unit test with the modules you wish to inject.
See next:
https://github.com/perracodex/Kcrud/blob/master/kcrud-base/src/test/kotlin/RbacServiceTest.kt
You will see a call to TestUtils.setupKoin()
in the unit test setUp()
method, which is configured to run before each test. For my particular test:
fun setupKoin(modules: List<Module> = emptyList()) {
val baseModules: List<Module> = listOf(
RbacInjection.get(),
ActorInjection.get()
)
startKoin {
modules(baseModules + modules)
}
}
See also:
https://github.com/perracodex/Kcrud/blob/master/kcrud-base/src/main/kotlin/kcrud/base/infrastructure/utils/TestUtils.kt
The key is to call next whenever you start a test, where modules
is the actual list of Koin modules to make available to the test.
startKoin {
modules(modules)
}
And do not forget to stop Koin on the test tearDown
Let me know if you need more clarification.Karim Houari
03/12/2024, 9:06 AM