Greetings. We are trying to write some tests for `...
# ktor
r
Greetings. We are trying to write some tests for
ktor
with
embeddedServer
using
mockk
library. And we are facing some problems with mocking objects. What is the best way to test
ktor
application? Assuming we need to mock some stuff that goes to third-party services? Any tough samples? Thanks.
c
You can use https://ktor.io/docs/testing.html to generate a temporary server that fakes HTTP actions, and a Client instance that calls it
If you want to fake a third-party service, you can just declare it as a route in that fake service, and the client will call it
r
We need to mock the exact object inside our app.
We already use
testApplication
with
createClient
DSLs.
r
Copy code
testApplication {

    //mock top level getStuff() func
    mockkStatic("my.package.name.ClassKt")
    every { getStuff() } returns listOf(Stuff(), Stuff())

    // mock class
    val repository = mockk<MyRepository>()
    val target = MyService(repository)
    every { repository.save(any()) } just Runs

}