MockK
What it is
MockK is a free, open-source mocking library for Kotlin, with dependency examples for Gradle and Maven and artifacts for JVM, Android unit tests, and Android instrumented tests. It supports spies, relaxed and partial mocks, object and class mocks, and constructor mocks. Mocks are strict by default, so tests must define the behavior they expect. For coroutine tests, coEvery and coVerify can stub and verify suspend functions. Optional BDD-style aliases include given, then, coGiven, and coThen. The documentation describes JUnit 4 rule support and a JUnit 5 extension, and links to SpringMockK and Quarkus-MockK support. From version 1.13.0, the library supports Kotlin 1.4 and higher. Restricted Mocking can prevent mocking classes considered problematic for reliable tests, including several Java system and collection classes restricted by default. The project identifies its license as Apache-2.0. Its documentation notes that inline functions cannot be mocked, that spies and mockkStatic may fail on JDK 16 and later, and that spies with suspending functions can produce unexpected results. JDK 21 and later may also show dynamic Java agent warnings.
For and against
For
- Supports object, class, constructor, and partial mocks.
- Coroutine APIs stub and verify suspend functions.
- Documents JUnit 4 and JUnit 5 integrations.
- Free and open source under Apache-2.0.
- Provides JVM and Android test artifacts.
Against
- Inline functions cannot be mocked.
- Spies and mockkStatic may fail on JDK 16 and later.
- Spies with suspending functions can produce unexpected results.
- JDK 21 and later may show dynamic-agent warnings.
The verdict
MockK offers a broad set of mocking options for Kotlin tests, with specific APIs for coroutines and integrations for common test setups. Its strict defaults and documented limitations are worth considering when choosing it for a test suite.
Who it suits
MockK suits Kotlin developers writing tests that need configurable stand-ins, including tests of suspend functions. Its JUnit support and Android test artifacts can fit projects using those environments.
On the record
- BDD
- Optional BDD-style aliases include given, then, coGiven, and coThen.mockk.io · read 4 Oct 2026
- BDD aliases
- MockK provides optional BDD style aliases such as given and then.mockk.io · read 11 Oct 2026
- Coroutines
- MockK supports stubbing and verifying suspend functions with coroutine-specific APIs, with the kotlinx-coroutines-core dependency.mockk.io · read 11 Oct 2026
- Distribution
- The setup guide lists JVM, Android unit-test and Android instrumented-test dependency artifacts.mockk.io · read 11 Oct 2026
- Framework integrations
- The site points to SpringMockK for Spring support and quarkus-mockk for mocking Quarkus beans.mockk.io · read 11 Oct 2026
- Framework support
- The site links to SpringMockK and Quarkus-MockK support.mockk.io · read 4 Oct 2026
- JDK caveats
- The documentation notes dynamic Java agent warnings on JDK 21+ and says spies or mockkStatic may not work on JDK 16+.mockk.io · read 11 Oct 2026
- Known issue
- The site notes that spies and mockkStatic may fail on JDK 16 and later with access exceptions.mockk.io · read 4 Oct 2026
- Known limitation
- Inline functions cannot be mocked, according to the known issues section.mockk.io · read 11 Oct 2026
- Kotlin compatibility
- The site says MockK supports Kotlin 1.4 and higher from version 1.13.0.mockk.io · read 4 Oct 2026
- Kotlin support
- The site says MockK supports Kotlin 1.4 and higher from MockK version 1.13.0.mockk.io · read 11 Oct 2026
- License
- The linked public GitHub repository identifies its license as Apache-2.0.github.com · read 4 Oct 2026
- Mock behavior
- Mocks are strict by default and require behavior to be provided.mockk.io · read 4 Oct 2026
- Mocking features
- The feature guide covers spies, relaxed and partial mocks, object, class, enum and constructor mocks, argument matching, and call verification.mockk.io · read 11 Oct 2026
- Purpose
- MockK is a mocking library for Kotlin.mockk.io · read 11 Oct 2026
- Security-related feature
- Restricted Mocking can prevent mocking classes considered problematic for reliable tests, and the documentation lists several Java system and collection classes restricted by default.mockk.io · read 4 Oct 2026
- Setup
- The documentation shows adding MockK as a test dependency with Gradle or Maven.mockk.io · read 11 Oct 2026
- Strict mocks
- Mocks are strict by default, so users need to provide behavior for them.mockk.io · read 11 Oct 2026