mockk /mockk

mocking library for Kotlin

★ 5,769⑂ 432👁 55⑃ 517● 1,068▤ 105
KotlinApache-2.0Created 2017-10-17mockk.io
下载:1.14.121.14.12 ↓1.14.122026-11-07
+0
日增长
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 Cat

Top issues

Discussions

Latest release

1.14.12 1.14.12 2026-11-07
What'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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON