Hi everyone! I’m currently trying to write some in...
# ktor
a
Hi everyone! I’m currently trying to write some integration tests and I need to actually mock cals to external APIs. There seems to be two ways of doing it: • using
externalServices
dsl in the
testApplication
(https://ktor.io/docs/testing.html#external-services) • using HttpClient
MockEngine
(https://ktor.io/docs/http-client-testing.html) the first comes from ktor server, and the second method comes from ktor client but the two seems to address the same goal. So my question is, what would be the main difference between the two ? Are there features one can do and the other can’t ? The objective is to intercept calls and just mock responses by returning fake JSON files stored locally. Thanks in advance!
a
The purpose of the
testApplication
is to write server tests without making network calls. The purpose of the
MockEngine
is to avoid making network requests within the tests which require the HTTP client usage.
I would use the
MockEngine
if you need to stub HTTP client requests.
a
Yes but, the
externalServices
block, in the example from the website, avoid making calls to the Google APIs by mocking them, isn’t it the same than using a
MockEngine
and mocking the calls too ? (Because we need to pass a
HttpClient
instance to the
oauth("auth-oauth-google") {
block, we could also pass a mocked HttpClient). I’m sorry I’m just confused if there is really a difference or if it’s just a matter of taste.
a
If you have a Ktor server then most likely you need the
testApplication
. If you only use the client then
MockEngine
is preferred way.
a
Alright. Well, yes, I forgot to mention it this is in the context of a Ktor API (Server) which needs to make external calls to other APIs using a special HttpClient instance.