0Pricing
Kotlin Multiplatform Academy · Lesson

Fake the Ktor Client & Repos

Use MockEngine and fakes for deterministic tests.

Fake the Ktor Client & Repos is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Fake the Ktor Client & Repos” lesson free?

Yes — the full text of “Fake the Ktor Client & Repos” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fake the Ktor Client & Repos”?

Use MockEngine and fakes for deterministic tests. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fake the Ktor Client & Repos” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. kotlin.test in commonTest
  2. Test suspend Functions & Flows
  3. Fake the Ktor Client & Repos
  4. Run Tests on Android & iOS Targets
← Back to Kotlin Multiplatform Academy