MockK is a Kotlin mocking library with spies, coroutine stubs, and optional BDD-style aliases for JVM and Android tests via Gradle or Maven.
Overview
MockK is a mocking framework tailored to Kotlin syntax and coroutines. Tests declare behavior with every or coEvery blocks, assert interactions with verify and coVerify, and may use given/then BDD aliases for readability.
Artifacts split across mockk-jvm, mockk-android, and mockk-agent packages for unit versus instrumented Android runs. Integrations are documented for Spring and Quarkus test stacks.
Strict mocks demand explicit stubbing by default; guides on Kotlin Academy and the MockK Guidebook cover matchers, static mocks, and object mocks with caveats for inline functions, JDK 21 agents, and JDK 16 reflective access limits.
Developers add test-scoped dependencies only; MockK ships as a library consumed inside test code, not a standalone application.
Key features
- Kotlin DSL mocking API
- Coroutine coEvery support
- JVM and Android artifacts
- BDD given/then aliases
- Spring and Quarkus hooks
- Strict mock defaults
- Static and object mocking
Use cases
- Kotlin unit test doubles
- Android instrumented mocks
- Coroutine service testing