Ktor Clientとリポジトリをフェイク化する
MockEngineとフェイクで決定性のあるテストを行います
「Ktor Clientとリポジトリをフェイク化する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Tests Should Not Hit the Network
Real HTTP calls are slow and flaky in tests. Instead you fake the response so each run is fast and deterministic. 🎯
Ktor Ships a MockEngine
Ktor includes a built-in MockEngine that returns canned responses instead of making real requests.
Build a Mock Client
Construct an HttpClient on top of MockEngine and decide exactly what bytes and status it replies with.
val client = HttpClient(MockEngine) {
engine { addHandler { respondOk("[]") } }
}Return Custom JSON
Inside the handler, call respond with the JSON body and headers your code expects to parse.
respond(
content = "{\"name\":\"Sam\"}",
headers = headersOf(HttpHeaders.ContentType, "application/json")
)Simulate Errors
Have the handler reply with a status like 500 to verify your error handling without a real server.
respond("oops", HttpStatusCode.InternalServerError)Inject the Client
Your repository should accept its HttpClient as a parameter, so tests pass the mock and production passes the real one.
class UserRepo(private val client: HttpClient)Why a Fake Repository
To test a ViewModel you do not even need Ktor: pass a fake repository that returns fixed data.
Fake an Interface
Because your repo hides behind an interface, a fake is just a small class implementing it with canned results.
class FakeRepo : UserRepo {
override suspend fun load(id: Int) = User("Sam")
}Control Failure Paths
A fake can also throw on demand, letting you test how callers react to a failing data layer.
override suspend fun load(id: Int): User =
throw IOException("offline")Fakes vs Mocks
A hand-written fake is simple and multiplatform-safe, while mocking libraries often lack full Kotlin/Native support.
Deterministic by Design
With MockEngine and fakes, every test controls its inputs, so results never depend on a network or shared state. ✅
Quick Check
Pick the right way to fake HTTP.
Recap
Swap real HTTP for MockEngine and inject fake repositories so tests stay fast, isolated, and predictable.
よくある質問
「Ktor Clientとリポジトリをフェイク化する」レッスンは無料ですか?
はい。「Ktor Clientとリポジトリをフェイク化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「Ktor Clientとリポジトリをフェイク化する」で何を学びますか?
MockEngineとフェイクで決定性のあるテストを行います ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Ktor Clientとリポジトリをフェイク化する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- commonTestのkotlin.test
- suspend関数とFlowをテストする
- Ktor Clientとリポジトリをフェイク化する
- AndroidとiOSのターゲットでテストを実行する