+0
Daily
5,769
stars
432
forks
55
watchers
517
prs
1,068
issues
105
releases
README
Getting started
All you need to get started is just to add a dependency to MockK library.
Gradle/Maven dependency
ApproachInstruction
testImplementation "io.mockk:mockk-jvm:${mockkVersion}"
(Kotlin DSL)
testImplementation("io.mockk:mockk-jvm:${mockkVersion}")
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>${mockkVersion}</version>
<scope>test</scope>
</dependency>
Unit
testImplementation "io.mockk:mockk-android:${mockkVersion}"
testImplementation "io.mockk:mockk-agent:${mockkVersion}"
Instrumented
androidTestImplementation "io.mockk:mockk-android:${mockkVersion}"
androidTestImplementation "io.mockk:mockk-agent:${mockkVersion}"
DSL examples
Simplest example. By default mocks are strict, so you need to provide some behaviour.
kotlin
val car = mockk()
every { car.drive(Direction.NORTH) } returns Outcome.OK
car.drive(Direction.NORTH) // returns OK
verify { car.drive(Direction.NORTH) }
confirmVerified(car)
See the "Features" section below for more detailed examples.
BDD style (optional)
For teams using Behavior-Driven Development, MockK provides BDD-style aliases
gradle
testImplementation "io.mockk:mockk:${mockkVersion}"
testImplementation "io.mockk:mockk-bdd:${mockkVersion}"
gradle
androidTestImplementation "io.mockk:mockk-android:${mockkVersion}"
androidTestImplementation "io.mockk:mockk-bdd-android:${mockkVersion}"
BDD aliases
| Standard MockK | BDD style |
|----------------|-----------|
| every { ... } | given { ... } |
| coEvery { ... } | coGiven { ... } |
| verify { ... } | then { ... } |
| coVerify { ... } | coThen { ... } |
Spring support
springmockk introduced in official Spring Boot Kotlin tutorial
Quarkus support
quarkus-mockk adds support for mocking beans in Quarkus. Documentation can be found here
Kotlin version support
From version 1.13.0 MockK supports Kotlin 1.4 and higher
Known issues
PowerMock needs a workaround to run together with MockK 79. (not sure after workaround if it is generally usable or not, please somebody report it)
Inline functions cannot be mocked: see the discussion on this issue
On JDK 21+, tests print WARNING: A Java agent has been loaded dynamically (JEP 451): read more here
Spies, mockkStatic may not work on JDK 16+; InaccessibleObjectException/IllegalAccessException: read more here
Table of contents:
auto-gen TOC:
{:toc}
Examples, guides & articles
Kotlin Academy articles
Check the series of articles "Mocking is not rocket science" at Kt. Academy describing MockK from the very basics of mocking up to description of all advanced features.
- Basics
- Expected behavior and behavior verification
- MockK features
- MockK advanced features
- Testing Quarkus with Kotlin, JUnit and MockK
- Unraveling MockK's black magic(EN, translation)
- Mockk Guidebook
- “Kotlin Unit Testing with Mockk” by Marco CatTop issues
#58Feature: Support Kotlin/Native in MockK52 comments · 2018-04-08#13Discussion: speed comparison against Mockito40 comments · 2017-12-26#1073Issue with mocking value classes with coEvery35 comments · 2023-04-01#466Android UI Test crashing on Android Emulator (but fine on physical devices) running Android P+ due to various SIGSEGV at random time34 comments · 2020-07-24#288Bug: exception while calling mocked suspend function29 comments · 2019-04-20#448re: java.lang.VerifyError on trying to mock an object25 comments · 2020-05-29#339Bug: for classes generated by Retrofit spies fail in coVerification clause on suspend functions25 comments · 2019-08-05#321Bug: ClassCastException when using relaxed mock of generic type24 comments · 2019-06-20
Discussions
#1599mockk-jvm and mockk-bdd-jvm not released together?1 comments · 2026-11-10#1498GitHub Sponsoring0 comments · 2026-01-14#1485mockk.junit.extension.requireParallelTesting what file should I put it?1 comments · 2025-12-24#1471Is MockK 1.14.7 requires minSdk 26?1 comments · 2025-12-06#1400How to make test failures with custom matchers easier to diagnose?1 comments · 2025-06-12#1397MockK Internal Implementation Explained!2 comments · 2025-05-18#1327How to inject spybeans in integration tests0 comments · 2024-12-08#1294Mockk performance while running large test suites0 comments · 2024-09-11
Latest release
1.14.12 1.14.12 2026-11-07What's Changed fix: added SameInstanceMatcher to handle an issue with equals method when using EqMatcher along with a mock to an object by @DOssaA in https://github.com/mockk/mockk/pull/1508 Add scoped confirmVerified overloads for callables (KFunction and KProperty) by @DOssaA in https://github.com/mockk/mockk/pull/1513 feat: support MockKAnnotation.useDependencyOrder for JUnit 5 by @ilia1243 in https://github.com/mockk/mockk/pull/1515 Allow updating settings from code by @Yentis in https://github.com/mockk/mockk/pull/1517 Add currentThread filter and excluded mocks to clearAllStubs