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:${mockkVersion}"
|
(Kotlin DSL) |
testImplementation("io.mockk:mockk:${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 { |


