GitHub logo mockk / mockk

mocking library for Kotlin

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

Approach Instruction
Gradle
testImplementation "io.mockk:mockk:${mockkVersion}"
Gradle (Kotlin DSL)
testImplementation("io.mockk:mockk:${mockkVersion}")
Maven
 <dependency&gt
     <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 {

View on GitHub