Good morning. I have a problem migrating my small ...
# kotest
d
Good morning. I have a problem migrating my small integration test suite to Kotest. I have a custom
MockAppExtension
(previously
MockAppRule
) that sets up Koin with Ktor’s `MockEngine`s and in-memory SqlDelight database. I have 3 test classes with 17 tests in total. If I run a single class, all of them pass with no problem, but if I run tests for the whole module, 2 out of 17 fail with
io.ktor.serialization.JsonConvertException: Illegal input
on a network request I wonder what could make the difference 🤔 I tried with different `IsolationMode`s
I have a convenience
object MockEngines
and in some tests I use
MockEngines.xyz.setHandler
, so on Kotest this same modified engine is persisted across all the tests, thus my successive api request receive an unexpected Json, as I don't set any constraints on that handler for simplicity of the code
d
I gave up on Ktor's mockserver, I found everything needs to be done manually... I ended up using WireMock with a kotlin wrapper lib I found, it encourages to write better tests and constraints.
d
I don’t have experience with WireMock, but I found Ktor-mock excellent! The MockEngine is not very versatile, but with a small trick, I’ve been able to: • Have “static” and isolated `MockEngine`s, like
TraktAuthEngine
,
TmdbMovieEngine
,etc, that can be reused easily for Unit tests • Merge multiple `MockEngine`s into a single one, to use for Integration tests • Add dynamic handlers to override a single request If you’re interested, here are my utils; still need some refactor, specially regarding
MockEngine.plus
where there is some code duplicated from below, but it does its job https://github.com/4face-studi0/CineScout/blob/main/cinescout/network/src/commonMain/kotlin/cinescout/network/testutil/ktorEngineUtils.kt
d
Nice! I tried starting with something like that, but I found it much easier to use this: https://github.com/marcinziolo/kotlin-wiremock
You get proper logs of requests/responses, simulation of network failures and lots more features of a more advanced mock server.
It's based on: https://wiremock.org/
d
Thanks! Looks cool!