DEV.co
Open-Source Testing · mockk

mockk

MockK is a Kotlin-native mocking library for unit testing that provides a DSL-based syntax for creating mocks, defining behavior, and verifying interactions. It supports both JVM and Android platforms with optional BDD-style aliases and integrates with Spring and Quarkus.

Source: GitHub — github.com/mockk/mockk
5.7k
GitHub stars
412
Forks
Kotlin
Primary language
Apache-2.0
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorymockk/mockk
Ownermockk
Primary languageKotlin
LicenseApache-2.0 — OSI-approved
Stars5.7k
Forks412
Open issues303
Latest releasev1.14.11 (2026-05-29)
Last updated2026-07-02
Sourcehttps://github.com/mockk/mockk

What mockk is

MockK uses bytecode manipulation and reflection (via an agent) to mock Kotlin classes, including support for coroutines, inline functions (with limitations), relaxed mocks, and spy objects. It distributes via Maven Central with separate artifacts for JVM (mockk-jvm) and Android (mockk-android + mockk-agent).

Quickstart

Get the mockk source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/mockk/mockk.gitcd mockk# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit Testing Kotlin Services

Primary use case for testing Kotlin business logic with strict verification of mock behavior via DSL syntax (every/verify). Works well in microservices and backend testing pipelines.

Android Unit and Instrumented Tests

Dedicated Android artifacts (mockk-android, mockk-agent) support both local JVM and on-device instrumented test scenarios. Preferred alternative to Mockito for Kotlin Android codebases.

TDD and BDD Workflows

Optional BDD module provides given/then aliases for behavior-driven test teams. Well-suited for projects adopting strict test-first development practices.

Implementation considerations

  • MockK requires inclusion of mockk-agent artifact on classpath/in buildpath to enable bytecode manipulation; agent setup varies between Gradle/Maven and IDE environments.
  • Strict mocks by default: all method calls must have explicit behavior defined via 'every { }' blocks or they throw exceptions; 'relaxed mocks' ease this but reduce verification strictness.
  • Coroutine support via coEvery/coVerify requires understanding of suspend functions and test scope lifecycle; test framework integration (JUnit 4/5) matters for proper coroutine context.
  • Spy objects (SpyK) partially delegate to real implementations; behavior differs from pure mocks and can lead to subtle test failures if real code has side effects.
  • Matrix testing and multiple Kotlin version support (1.4+) requires explicit dependency version pinning; version mismatches can cause bytecode loading failures.

When to avoid it — and what to weigh

  • Heavy Reliance on PowerMock Patterns — Known compatibility issues with PowerMock; workarounds exist but general usability after mitigation is unclear per README. If your codebase heavily depends on PowerMock's advanced features, consider isolation or alternatives.
  • Extensive Inline Function Mocking — Inline functions cannot be mocked in Kotlin due to language-level compilation; MockK cannot work around this. If your architecture relies heavily on inlined utility functions, mocking becomes impossible.
  • JDK 16+ with Static Mocks — mockkStatic and spies may fail on JDK 16+ with InaccessibleObjectException due to Java module system restrictions. Workarounds documented but require additional setup; Java 8–11 environments preferred.
  • Non-Kotlin Codebases — MockK is Kotlin-specific and optimized for Kotlin semantics. Java-only projects should use Mockito, EasyMock, or similar Java mocking frameworks.

License & commercial use

MockK is licensed under Apache License 2.0 (Apache-2.0), a permissive OSI-approved open-source license that permits commercial use, modification, and distribution with attribution and liability disclaimer.

Apache 2.0 explicitly permits commercial use in proprietary products. No license fees, vendor lock-in, or per-seat restrictions. Code modifications can be closed-source if redistribution is not required. Consult your legal team if bundling MockK binaries in a commercial product offering; Apache 2.0 requires license/attribution notices in distribution.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

MockK uses bytecode manipulation and reflection via an agent loaded on JVM startup; review agent code if running in high-security environments (e.g., financial, healthcare). No security vulnerabilities disclosed in README. Agent operates only during test execution (test scope), not in production. Verify dependency integrity via Maven Central checksums. Third-party Spring/Quarkus extensions (springmockk, quarkus-mockk) should be vetted separately.

Alternatives to consider

Mockito

Mature Java mocking framework with limited Kotlin ergonomics; does not leverage Kotlin language features (extension functions, coroutines) as naturally as MockK. Preferred for Java-centric teams but less idiomatic in Kotlin codebases.

EasyMock

Record/replay model less intuitive for modern TDD workflows compared to MockK's DSL. Primarily Java-focused; weaker Kotlin support. Use case: teams with existing EasyMock infrastructure reluctant to migrate.

JMockit

Supports static/final method mocking via bytecode manipulation but heavier overhead and steeper learning curve than MockK. Declining adoption in Kotlin ecosystem; use case: legacy codebases requiring static mock capabilities unavailable in MockK.

Software development agency

Build on mockk with DEV.co software developers

MockK delivers Kotlin-native mocking with strong community adoption and active maintenance. Start with the quick-start Gradle dependency and Kotlin Academy guides. For Spring/Quarkus integration, review springmockk and quarkus-mockk separately. Contact Devco for custom test infrastructure assessment.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

mockk FAQ

Can I use MockK in production code?
No. MockK is a test-scoped library (testImplementation/test scope). It is not designed or packaged for runtime use. Attempting to use it in production will fail due to bytecode agent dependencies and test-only APIs.
Do I need to download and manage the MockK agent separately?
No. When you add mockk-agent as a test dependency, your build tool (Gradle/Maven) manages the classpath automatically. IDEs and CI systems typically detect and load it transparently. Manual -javaagent flags are rarely necessary unless running tests outside standard build tools.
What is the difference between a strict mock and a relaxed mock?
Strict mocks (default) throw exceptions if unmocked methods are called; every interaction must be explicitly configured with 'every { }'. Relaxed mocks return default values (Unit for void, null for objects) for unconfigured methods, reducing setup verbosity but lowering verification rigor.
Why can't MockK mock inline functions?
Kotlin inline functions are inlined at compile time into the bytecode of the caller. By the time the JVM executes the code, the function no longer exists as a distinct callable—it is part of the caller's bytecode. MockK's agent cannot intercept a function that does not exist at runtime.

Software developers & web developers for hire

Need help beyond evaluating mockk? DEV.co is a software development agency offering software development services and web development for teams of every size. Our software developers and web developers build custom software, web applications, APIs, and open-source testing integrations — and maintain them long-term.

Evaluate MockK for Your Kotlin Test Suite

MockK delivers Kotlin-native mocking with strong community adoption and active maintenance. Start with the quick-start Gradle dependency and Kotlin Academy guides. For Spring/Quarkus integration, review springmockk and quarkus-mockk separately. Contact Devco for custom test infrastructure assessment.