drawing drawing

Release Version Change log codecov Android Matrix tests Open Source Helpers

Getting started

All you need to get started is just to add a dependency to MockK library.

Gradle/Maven dependency

ApproachInstruction
Gradle
testImplementation "io.mockk:mockk-jvm:${mockkVersion}"
Gradle (Kotlin DSL)
testImplementation("io.mockk:mockk-jvm:${mockkVersion}")
Maven
 <dependency>
     <groupId>io.mockk</groupId>
     <artifactId>mockk-jvm</artifactId>
     <version>${mockkVersion}</version>
     <scope>test</scope>
 </dependency>
android Unit
testImplementation "io.mockk:mockk-android:${mockkVersion}"
testImplementation "io.mockk:mockk-agent:${mockkVersion}"
android 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.

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

Quarkus support

Kotlin version support

From version 1.13.0 MockK supports Kotlin 1.4 and higher

Known issues

Table of contents:

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.

Japanese guides and articles

Chinese guides and articles

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