Dmytro Koval
11/07/2024, 1:18 PMsingle
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:
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()
}
}
}
}
}
Emil Kantis
11/07/2024, 6:29 PMDmytro Koval
11/08/2024, 11:56 AMEmil Kantis
11/09/2024, 10:27 AM