Getting started
All you need to get started is just to add a dependency to MockK library.
Gradle/Maven dependency
| Approach | Instruction |
|---|---|
![]() |
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>
|
testImplementation "io.mockk:mockk-android:${mockkVersion}"
testImplementation "io.mockk:mockk-agent:${mockkVersion}"
|
|
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.
val car = mockk<Car>()
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
testImplementation "io.mockk:mockk:${mockkVersion}"
testImplementation "io.mockk:mockk-bdd:${mockkVersion}"
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,
mockkStaticmay not work on JDK 16+;InaccessibleObjectException/IllegalAccessException: read more here - Using a spy with a suspending function will give unexpected test results
Table of contents:
- Getting started
- DSL examples
- BDD style (optional)
- Examples, guides & articles
- Features
- Annotations
- @InjectMockKs dependency order
- JUnit4
- JUnit5
- Spy
- Relaxed mock
- Partial mocking
- Mock relaxed for functions returning Unit
- Object mocks
- Class mock
- Enumeration mocks
- Constructor mocks
- Partial argument matching
- Chained calls
- Hierarchical mocking
- Capturing
- Verification atLeast, atMost or exactly times
- Verification order
- Verification confirmation
- Unnecessary stubbing
- Recording exclusions
- Verification timeout
- Returning Unit
- Coroutines
- Top Level functions
- Extension functions
- Varargs
- Private functions mocking / dynamic calls
- Property backing fields
- Multiple interfaces
- Mocking Nothing
- Clearing vs Unmocking
- Scoped mocks
- Suppressing superclass calls
- Matcher extensibility
- Configuration File
- DSL tables
- Restricted Mocking in MockK
- Funding
- Getting Help
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
- Testing Quarkus with Kotlin, JUnit and MockK
- Unraveling MockK’s black magic(EN, translation)
- Mockk Guidebook
- “Kotlin Unit Testing with Mockk” by Marco Cattaneo
- (Video) Use verify in MockK to validate function calls on mocked object
- Testing With MockK paid course on raywenderlich.com
- TDD for Android video tutorial part 1, part 2 by Ryan Kay
- (Video)Android Developer Live Coding #13: Unit Testing with Mockk, Coroutines, Test Driven Development
- KotlinConf 2018 - Best Practices for Unit Testing in Kotlin by Philipp Hauer
- kotlin-fullstack-sample uses MockK project covered with tests
- DZone article
- Habrahabr article (RU)
- Mocking in Kotlin with MockK - Yannick De Turck
- How does MockK work internally? - Sagar Malhotra
Japanese guides and articles
- Documentation translation to Japanese
- Unraveling MockK’s black magic / MockKの「黒魔術」を解明する (JP, but readable through chrome translator)
Chinese guides and articles
- 用 Kotlin + Mockito 寫單元測試會碰到什麼問題?
- MockK 功能介紹:mockk, every, Annotation, verify
- MockK 功能介紹:Relaxed Mocks, 再談 Verify, Capture
- 如何測試 Static Method, Singleton
Korean guides and articles
Features
Annotations
You can use annotations to simplify the creation of mock objects:
class TrafficSystem {
lateinit var car1: Car
lateinit var car2
