Hey! I’m struggling a bit making Kotest, Koin and ...
# kotest
d
Hey! I’m struggling a bit making Kotest, Koin and Ktor work together. Specifically overriding a
single
defined in the main application with a
Mockk
in tests. I was expecting Koin to pickup the
customerServiceMock
instead of the production
CustomerService
, but it doesn’t. Here’s a sample of the test:
Copy code
class APITest :
    FunSpec(),
    KoinTest {

    override fun extensions(): List<Extension> = listOf(
        WireMockListener(jwksWireMockServer, ListenerMode.PER_SPEC),
        KoinExtension(
            module = module {},
            mockProvider = { mockkClass(it, relaxed = true) },
            mode = KoinLifecycleMode.Root,
        ),
    )

    init {
        val testDb = install(DatabaseTestConfig.jdbcTestExtension).createTestDatabase()

        val appConfig =
            listOf(
                ApplicationConfig("application.test.conf"),
                testDb.applicationConfig,
                JwksWireMockSetup.appConfig,
            ).reduce(ApplicationConfig::mergeWith)

        test("Empty tables and no defaults should return empty results") {
            testApplication {
                environment {
                    config = appConfig
                }

                val customerServiceMock = declareMock<CustomerService>()

                coEvery { customerServiceMock.getCustomer(any()) }.returns(Customer(CustomerId("1"), 30))

                val cli = createTestClient()

                cli.get("/api/v1/recommendations?customerId=1").apply {
                    status shouldBe HttpStatusCode.OK
                    body<Recommendations>() shouldBe Recommendations.empty()
                }
            }
        }
    }
}
e
Can you provide a small reproduction sample?
d
Hey @Emil Kantis Here’s the reproduction sample: https://github.com/mityakoval/KoinKotestSample I think the issue is with how the routes are set up.
e
Thanks @Dmytro Koval, I had a look into it but it's not obvious to me how Ktor starts Koin and how/if it supports testing. https://insert-koin.io/docs/reference/koin-ktor/ktor/ mentions nothing regarding testing. Do you know if it's supported at all at this point?