Freedom251 › Unit Testing Frameworks
MockK

What MockK is
MockK is a free, open-source mocking library for Kotlin. It provides mocks, spies, relaxed and partial mocks, object and class mocks, and constructor mocks for tests. Mocks are strict by default, so expected behavior must be defined. Coroutine APIs such as coEvery and coVerify support stubbing and verification of suspend functions. Optional BDD aliases include given, then, coGiven, and coThen. Documentation covers JUnit 4 rules, a JUnit 5 extension, and integrations through SpringMockK and quarkus-mockk. Setup examples are provided for Gradle and Maven, with artifacts for JVM, Android unit tests, and Android instrumented tests. MockK supports Kotlin 1.4 and later from version 1.13.0, and is listed for Android, Linux, macOS, and Windows. Inline functions cannot be mocked. Spies and mockkStatic may fail with access exceptions on JDK 16 and later; JDK 21 and later may show dynamic-agent warnings.
Who it suits
MockK suits Kotlin developers writing unit or integration tests across JVM and Android projects. It is useful when tests need coroutine support, BDD aliases, or several mock types.
What it does well
- Free and open source
- Supports coroutine stubbing and verification
- Includes JUnit 4 and JUnit 5 integrations
- Provides many mock types
- Offers Spring and Quarkus integrations
What to know first
- Inline functions cannot be mocked
- Spies may fail on JDK 16 and later
- JDK 21+ may show agent warnings
- Strict mocks require defined behavior
Verdict
MockK offers broad mocking features and Kotlin test integrations at no cost. Its strict defaults and documented JDK and inline-function limitations should be considered when setting up tests.
Facts from MockK's own pages
- BDD
- Optional BDD-style aliases include given, then, coGiven, and coThen. · mockk.io, 4 Oct 2026
- BDD aliases
- MockK provides optional BDD style aliases such as given and then. · mockk.io, 11 Oct 2026
- Coroutines
- MockK supports stubbing and verifying suspend functions with coroutine-specific APIs, with the kotlinx-coroutines-core dependency. · mockk.io, 11 Oct 2026
- Distribution
- The setup guide lists JVM, Android unit-test and Android instrumented-test dependency artifacts. · mockk.io, 11 Oct 2026
- Framework integrations
- The site points to SpringMockK for Spring support and quarkus-mockk for mocking Quarkus beans. · mockk.io, 11 Oct 2026
- Framework support
- The site links to SpringMockK and Quarkus-MockK support. · mockk.io, 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, 11 Oct 2026
- Known issue
- The site notes that spies and mockkStatic may fail on JDK 16 and later with access exceptions. · mockk.io, 4 Oct 2026
- Known limitation
- Inline functions cannot be mocked, according to the known issues section. · mockk.io, 11 Oct 2026
- Kotlin compatibility
- The site says MockK supports Kotlin 1.4 and higher from version 1.13.0. · mockk.io, 4 Oct 2026
- Kotlin support
- The site says MockK supports Kotlin 1.4 and higher from MockK version 1.13.0. · mockk.io, 11 Oct 2026
- License
- The linked public GitHub repository identifies its license as Apache-2.0. · github.com, 4 Oct 2026
- Mock behavior
- Mocks are strict by default and require behavior to be provided. · mockk.io, 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, 11 Oct 2026
- Purpose
- MockK is a mocking library for Kotlin. · mockk.io, 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, 4 Oct 2026
- Setup
- The documentation shows adding MockK as a test dependency with Gradle or Maven. · mockk.io, 11 Oct 2026
- Strict mocks
- Mocks are strict by default, so users need to provide behavior for them. · mockk.io, 11 Oct 2026
- Support
- The project directs users to Stack Overflow or Gitter for questions and GitHub for bug reports. · mockk.io, 11 Oct 2026
- Suspending spy caveat
- The site warns that using a spy with a suspending function can produce unexpected test results. · mockk.io, 11 Oct 2026
- Test integrations
- The documentation describes JUnit 4 and JUnit 5 support, including a JUnit 5 extension for initializing mocks. · mockk.io, 11 Oct 2026
Where MockK stands on Freedom251
- Unit Testing Frameworks#21 of 32
