dbaelz
11/01/2024, 8:48 PMloadKoinModules()
, but that didn't work. See minimal code example in the thread. Is it possible to test that way? Is there another approach to do that?dbaelz
11/01/2024, 8:49 PMclass MinimalTest {
private val experienceRepository = mockk<ExperienceRepository>()
private val koinTestModule = module {
single<ExperienceRepository> { experienceRepository }
}
@Test
fun minimalTest() = testApplication {
val experience = listOf(
Experience(0, LocalDateTime.now(), 1, "reason1"),
Experience(1, LocalDateTime.now(), 2, "reason2")
)
coEvery { experienceRepository.getExperience() } returns experience
application {
// The module extension function calls install(Koin) and
// also injects the ExperienceRepository
module()
// Use the mocked ExperienceRepository instead
loadKoinModules(koinTestModule)
}
// Internally the Ktor Server use the routing plugin and
// then access the data with the ExperienceRepository.
// It should use the mocked ExperienceRepository, but uses the production one
val response = client.get(EXPERIENCE.fullResourcePath)
val actual = Json.decodeFromString(ExperienceDTO.serializer(), response.bodyAsText())
assert(actual.entries.isNotEmpty())
}
}
Dmytro Koval
11/07/2024, 1:07 PMdbaelz
11/08/2024, 3:02 PMApplication.module()
function and therefore loadKoinModules
provides the mock, but it's never used.
Due Koin issues I can't inject in Route extension function where I needed the repository provided with Koin. Therefore, I did hand over the modules as parameter ListModule in Application.module(). For production only the product module(s), for testing production + the koinTestModule that overrides the repo with the mockk. That works because when handing over multiple modules in Koin existing definitions of one module can be overridden by another.