MockK
Kotlin-native mocking library for JVM and Android. MockK uses Kotlin's reflection and coroutine support to provide idiomatic mocking — mockk<T>(), every { }, verify { }, coEvery { } for suspend functions. Unlike Mockito, MockK was built for Kotlin from the ground up: supports data classes, extension functions, object mocks, top-level function mocks, and coroutine-native coEvery/coVerify. The standard choice for mocking in Kotlin projects.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Test library — no direct security surface. Runs in JVM test sandbox. No network or credential handling.
⚡ Reliability
Best When
You're writing Kotlin unit tests for code that uses coroutines, data classes, companion objects, or other Kotlin-specific patterns that Mockito handles poorly.
Avoid When
Your codebase is Java — Mockito has better Java ergonomics and larger Java ecosystem support.
Use Cases
- • Mock agent dependencies (API clients, repositories, services) in Kotlin unit tests with every { } stubbing and verify { } assertion blocks
- • Mock suspend functions and coroutine-based agent interfaces with coEvery { } and coVerify { } without runBlocking wrapper complexity
- • Mock companion objects, object singletons, and top-level Kotlin functions that Mockito cannot mock — critical for testing Kotlin idioms
- • Capture arguments passed to mocked agent methods using CapturingSlot<T> to assert on complex request objects
- • Use relaxed mocks (mockk<T>(relaxed = true)) for agent tests where you only care about specific interactions, not all method calls
Not For
- • Java-only projects — Mockito is better suited for Java; MockK's Kotlin DSL is awkward in Java
- • Android instrumented tests requiring framework mocking — Robolectric + Mockito handles Android framework mocking; MockK is for unit tests
- • Final class mocking in Java projects without inline mock maker — MockK handles Kotlin finals natively but Java finales need extra config
Interface
Authentication
Test library — no authentication. Runs in JVM test environment.
Pricing
Community-maintained open source. Apache 2.0 license.
Agent Metadata
Known Gotchas
- ⚠ MockK mocks are not thread-safe by default — tests that use mock in multiple coroutines or threads concurrently may produce race conditions in verify { } counts
- ⚠ every { } stubbing uses reference equality by default — if your code creates new argument objects, use any() or custom matchers to avoid 'no answer found' NoSuchElementException
- ⚠ Android instrumented tests require mockk-android artifact, not mockk — using the wrong artifact causes ClassNotFoundException or runtime crashes on Android
- ⚠ mockkStatic and mockkObject require explicit unmockkStatic / unmockkAll cleanup after tests — stale static mocks leak between tests causing ordering-dependent failures
- ⚠ coEvery { } requires that the tested function is actually a suspend function — using coEvery on non-suspend functions throws UnnecessaryStubbingException or silently uses wrong stub
- ⚠ verify { exactly = 0 } works differently from verifyNever — and excludeRecords { } must be used to prevent verify(exactly = 0) from counting indirect mock interactions
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for MockK.
Scores are editorial opinions as of 2026-03-06.