Mocking with MockK
Mock dependencies.
Mocking with MockK is a free Kotlin 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is MockK?
MockK is a mocking library built for Kotlin. It creates fake versions of dependencies so you can test a class in isolation.
- First-class Kotlin support (objects, extensions, coroutines)
- Readable DSL for stubbing and verifying
Adding MockK
Add MockK as a test dependency.
dependencies {
testImplementation("io.mockk:mockk:1.13.11")
}Creating a Mock
Use mockk<Type>() to create a mock. By default its methods are not stubbed and will fail if called.
import io.mockk.mockk
interface Repo { fun find(id: Int): String }
val repo = mockk<Repo>()Stubbing with every
Define behavior using every { } returns .... This tells the mock what to return for a given call.
import io.mockk.every
import io.mockk.mockk
interface Repo { fun find(id: Int): String }
val repo = mockk<Repo>()
// every { repo.find(1) } returns "Ada"Verifying Calls
Use verify { } to assert a method was called. You can require an exact count.
import io.mockk.verify
// verify { repo.find(1) }
// verify(exactly = 2) { repo.find(any()) }
// verify(exactly = 0) { repo.find(99) }Argument Matchers
Matchers like any(), eq(), and match { } make stubs and verifications flexible.
import io.mockk.every
import io.mockk.mockk
interface Repo { fun find(id: Int): String }
val repo = mockk<Repo>()
// every { repo.find(any()) } returns "X"
// every { repo.find(match { it > 10 }) } returns "big"Relaxed Mocks
A relaxed mock returns sensible defaults (0, empty string, etc.) for unstubbed calls, so you don't have to stub everything.
import io.mockk.mockk
interface Logger { fun log(msg: String) }
val logger = mockk<Logger>(relaxed = true)Capturing Arguments
Use a slot with capture() to inspect the exact argument passed to a mocked method.
import io.mockk.slot
import io.mockk.verify
val msgSlot = slot<String>()
// verify { logger.log(capture(msgSlot)) }
// println(msgSlot.captured)Spies
A spyk wraps a real object: real methods run unless you stub them. Useful for partial mocking.
import io.mockk.spyk
class Calc { fun square(n: Int) = n * n }
val calc = spyk(Calc())
// real square runs; you can still verify { calc.square(3) }Throwing from a Mock
Stub a method to throw, so you can test error handling paths.
import io.mockk.every
import io.mockk.mockk
interface Repo { fun find(id: Int): String }
val repo = mockk<Repo>()
// every { repo.find(0) } throws NoSuchElementException()Cleaning Up
Call clearAllMocks() in @AfterEach, or use the JUnit5 MockKExtension, to reset mock state between tests and avoid leakage.
import io.mockk.clearAllMocks
import org.junit.jupiter.api.AfterEach
class ServiceTest {
@AfterEach
fun tearDown() { clearAllMocks() }
}Quick Check
You have a mock with many methods but only care about one. You want unstubbed methods to return harmless defaults. What do you create?
Recap
MockK lets you isolate code under test:
mockk()creates mocks,every { } returnsstubs themverify { }checks interactions- Matchers,
slotcapture,spyk, relaxed mocks clearAllMocks()between tests
Next: testing coroutines.
Frequently asked questions
Is the “Mocking with MockK” lesson free?
Yes — the full text of “Mocking with MockK” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.
What will I learn in “Mocking with MockK”?
Mock dependencies. You practise Kotlin 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 Academy?
No prior experience is required. Kotlin 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 “Mocking with MockK” 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 Academy lesson?
Yes. Every Kotlin 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
- Writing Tests with JUnit5
- Assertions
- Mocking with MockK
- Coroutine Testing