Comprehensive mocking library for Kotlin with native coroutine support and advanced DSL features.
npx @tessl/cli install tessl/maven-io-mockk--mockk@1.14.00
# MockK
1
2
MockK is a comprehensive mocking library specifically designed for Kotlin that provides native support for Kotlin language features including coroutines, extension functions, data classes, and object mocks. It offers a rich DSL for creating mocks, spies, and stubs with advanced capabilities like relaxed mocking, partial mocking, verification with call ordering, argument capturing, and hierarchical mocking.
3
4
## Package Information
5
6
- **Package Name**: io.mockk:mockk
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Installation**: `implementation("io.mockk:mockk:1.14.3")`
10
11
## Core Imports
12
13
```kotlin
14
import io.mockk.*
15
```
16
17
For specific functionality:
18
19
```kotlin
20
import io.mockk.mockk
21
import io.mockk.spyk
22
import io.mockk.every
23
import io.mockk.verify
24
import io.mockk.slot
25
```
26
27
## Basic Usage
28
29
```kotlin
30
import io.mockk.*
31
32
// Create a mock
33
interface UserService {
34
fun getUserById(id: String): User
35
fun saveUser(user: User): Boolean
36
}
37
38
val userService = mockk<UserService>()
39
40
// Stub behavior
41
every { userService.getUserById("123") } returns User("123", "John")
42
every { userService.saveUser(any()) } returns true
43
44
// Use the mock
45
val user = userService.getUserById("123")
46
userService.saveUser(user)
47
48
// Verify interactions
49
verify { userService.getUserById("123") }
50
verify { userService.saveUser(any()) }
51
```
52
53
## Architecture
54
55
MockK is built around several key components:
56
57
- **Mock Creation**: Core functions (`mockk`, `spyk`) for creating different types of test doubles
58
- **DSL Scopes**: Fluent interfaces (`MockKMatcherScope`, `MockKStubScope`, `MockKVerificationScope`) for composing test scenarios
59
- **Stubbing Engine**: Behavior definition system with extensive answer types and matchers
60
- **Verification System**: Comprehensive call verification with ordering, counting, and timeout support
61
- **Platform Integration**: Multiplatform support with JVM-specific features for static and constructor mocking
62
- **Annotation Support**: Automatic mock initialization through annotations and JUnit integration
63
64
## Capabilities
65
66
### Mock Creation
67
68
Core mock and spy creation functions with comprehensive configuration options for different testing scenarios.
69
70
```kotlin { .api }
71
inline fun <reified T : Any> mockk(
72
name: String? = null,
73
relaxed: Boolean = false,
74
vararg moreInterfaces: KClass<*>,
75
relaxUnitFun: Boolean = false,
76
mockValidator: MockkValidator = MockkValidator(RestrictMockkConfiguration()),
77
block: T.() -> Unit = {}
78
): T
79
80
inline fun <reified T : Any> spyk(
81
name: String? = null,
82
vararg moreInterfaces: KClass<*>,
83
recordPrivateCalls: Boolean = false,
84
block: T.() -> Unit = {}
85
): T
86
87
inline fun <reified T : Any> spyk(
88
objToCopy: T,
89
name: String? = null,
90
vararg moreInterfaces: KClass<*>,
91
recordPrivateCalls: Boolean = false,
92
block: T.() -> Unit = {}
93
): T
94
```
95
96
[Mock Creation](./mock-creation.md)
97
98
### Stubbing
99
100
Behavior definition system for specifying how mocks should respond to method calls, with support for complex scenarios and coroutines.
101
102
```kotlin { .api }
103
fun <T> every(stubBlock: MockKMatcherScope.() -> T): MockKStubScope<T, T>
104
fun <T> coEvery(stubBlock: suspend MockKMatcherScope.() -> T): MockKStubScope<T, T>
105
fun justRun(stubBlock: MockKMatcherScope.() -> Unit)
106
fun coJustRun(stubBlock: suspend MockKMatcherScope.() -> Unit)
107
```
108
109
[Stubbing](./stubbing.md)
110
111
### Verification
112
113
Comprehensive call verification system with support for call ordering, counting, timeouts, and complex verification patterns.
114
115
```kotlin { .api }
116
fun verify(
117
ordering: Ordering = Ordering.UNORDERED,
118
inverse: Boolean = false,
119
atLeast: Int = 1,
120
atMost: Int = Int.MAX_VALUE,
121
exactly: Int = -1,
122
timeout: Long = 0,
123
verifyBlock: MockKVerificationScope.() -> Unit
124
)
125
126
fun coVerify(
127
ordering: Ordering = Ordering.UNORDERED,
128
inverse: Boolean = false,
129
atLeast: Int = 1,
130
atMost: Int = Int.MAX_VALUE,
131
exactly: Int = -1,
132
timeout: Long = 0,
133
verifyBlock: suspend MockKVerificationScope.() -> Unit
134
)
135
```
136
137
[Verification](./verification.md)
138
139
### Argument Matching
140
141
Sophisticated argument matching system with built-in matchers for equality, comparison, type checking, capturing, and logical operations.
142
143
```kotlin { .api }
144
// In MockKMatcherScope context
145
fun <T> any(): T
146
fun <T> eq(value: T): T
147
fun <T> capture(slot: CapturingSlot<T>): T
148
fun <T> match(matcher: Matcher<T>): T
149
```
150
151
[Matchers](./matchers.md)
152
153
### Special Mocking
154
155
Advanced mocking capabilities for static methods, object instances, and constructors, enabling testing of complex Kotlin constructs.
156
157
```kotlin { .api }
158
inline fun mockkStatic(vararg classes: KClass<*>)
159
inline fun mockkObject(vararg objects: Any, recordPrivateCalls: Boolean = false)
160
inline fun mockkConstructor(
161
vararg classes: KClass<*>,
162
recordPrivateCalls: Boolean = false,
163
localToThread: Boolean = false
164
)
165
```
166
167
[Special Mocking](./special-mocking.md)
168
169
### Annotations
170
171
Annotation-based mock initialization system with automatic dependency injection and JUnit integration for streamlined test setup.
172
173
```kotlin { .api }
174
@MockK
175
@RelaxedMockK
176
@SpyK
177
@InjectMockKs
178
```
179
180
[Annotations](./annotations.md)
181
182
## Core Types
183
184
```kotlin { .api }
185
interface Matcher<in T> {
186
fun match(arg: T?): Boolean
187
}
188
189
class CapturingSlot<T> {
190
val captured: T
191
val isCaptured: Boolean
192
fun clear()
193
}
194
195
interface Answer<out T> {
196
fun answer(call: Call): T
197
suspend fun coAnswer(call: Call): T
198
}
199
200
enum class Ordering {
201
UNORDERED, ALL, ORDERED, SEQUENCE
202
}
203
204
class MockkValidator(configuration: RestrictMockkConfiguration)
205
206
class RestrictMockkConfiguration
207
208
interface Call {
209
val invocation: Invocation
210
val matcher: InvocationMatcher
211
}
212
213
interface Invocation {
214
val self: Any
215
val method: MethodDescription
216
val args: List<Any?>
217
}
218
219
interface MethodDescription {
220
val name: String
221
val returnType: kotlin.reflect.KClass<*>
222
val declaringClass: kotlin.reflect.KClass<*>
223
}
224
```