0
# Mock Creation
1
2
Type-safe mock creation with extensive configuration options and immediate stubbing support. Essential for setting up test doubles in Kotlin tests.
3
4
## Capabilities
5
6
### Basic Mock Creation
7
8
Creates a mock for the specified type with optional configuration parameters.
9
10
```kotlin { .api }
11
/**
12
* Creates a mock for the specified reified type T
13
* @param extraInterfaces Additional interfaces the mock should implement
14
* @param name Mock name for debugging purposes
15
* @param spiedInstance Instance to spy on (for partial mocks)
16
* @param defaultAnswer Default answer for unstubbed methods
17
* @param serializable Whether the mock should be serializable
18
* @param serializableMode Specific serialization mode
19
* @param verboseLogging Enable real-time method invocation logging
20
* @param invocationListeners Listeners for method invocations
21
* @param stubOnly Stub-only mock saves memory but disallows verification
22
* @param useConstructor Attempt to use constructor when creating mock instance
23
* @param outerInstance For mocking non-static inner classes with useConstructor
24
* @param lenient Bypass strict stubbing validation
25
* @returns Mock instance of type T
26
*/
27
inline fun <reified T : Any> mock(
28
extraInterfaces: Array<out KClass<out Any>>? = null,
29
name: String? = null,
30
spiedInstance: Any? = null,
31
defaultAnswer: Answer<Any>? = null,
32
serializable: Boolean = false,
33
serializableMode: SerializableMode? = null,
34
verboseLogging: Boolean = false,
35
invocationListeners: Array<InvocationListener>? = null,
36
stubOnly: Boolean = false,
37
useConstructor: UseConstructor? = null,
38
outerInstance: Any? = null,
39
lenient: Boolean = false
40
): T
41
```
42
43
**Usage Examples:**
44
45
```kotlin
46
import com.nhaarman.mockitokotlin2.mock
47
48
// Basic mock
49
val userService = mock<UserService>()
50
51
// Named mock for debugging
52
val namedMock = mock<UserService>(name = "userServiceMock")
53
54
// Mock with default answer
55
val mockWithAnswer = mock<UserService>(
56
defaultAnswer = Answer { "default response" }
57
)
58
```
59
60
### Mock with Immediate Stubbing
61
62
Creates a mock with immediate stubbing configuration using a lambda DSL.
63
64
```kotlin { .api }
65
/**
66
* Creates a mock with immediate stubbing configuration
67
* @param stubbing Lambda for configuring mock behavior immediately after creation
68
* @returns Configured mock instance of type T
69
*/
70
inline fun <reified T : Any> mock(
71
extraInterfaces: Array<out KClass<out Any>>? = null,
72
name: String? = null,
73
spiedInstance: Any? = null,
74
defaultAnswer: Answer<Any>? = null,
75
serializable: Boolean = false,
76
serializableMode: SerializableMode? = null,
77
verboseLogging: Boolean = false,
78
invocationListeners: Array<InvocationListener>? = null,
79
stubOnly: Boolean = false,
80
useConstructor: UseConstructor? = null,
81
outerInstance: Any? = null,
82
lenient: Boolean = false,
83
stubbing: KStubbing<T>.(T) -> Unit
84
): T
85
```
86
87
**Usage Examples:**
88
89
```kotlin
90
import com.nhaarman.mockitokotlin2.*
91
92
// Mock with immediate stubbing
93
val userService = mock<UserService> {
94
on { findUser(any()) } doReturn User("John", "john@example.com")
95
on { isUserActive(any()) } doReturn true
96
}
97
98
// Complex stubbing with multiple responses
99
val calculator = mock<Calculator> {
100
on { add(any(), any()) } doAnswer { invocation ->
101
val a = invocation.getArgument<Int>(0)
102
val b = invocation.getArgument<Int>(1)
103
a + b
104
}
105
on { divide(any(), 0) } doThrow ArithmeticException("Division by zero")
106
}
107
```
108
109
### Mock Settings Configuration
110
111
Creates MockSettings with optional parameters for advanced mock configuration.
112
113
```kotlin { .api }
114
/**
115
* Creates MockSettings with optional parameters for advanced mock configuration
116
* @param extraInterfaces Additional interfaces the mock should implement
117
* @param name Mock name for debugging purposes
118
* @param spiedInstance Instance to spy on (for partial mocks)
119
* @param defaultAnswer Default answer for unstubbed methods
120
* @param serializable Whether the mock should be serializable
121
* @param serializableMode Specific serialization mode
122
* @param verboseLogging Enable real-time method invocation logging
123
* @param invocationListeners Listeners for method invocations
124
* @param stubOnly Stub-only mock saves memory but disallows verification
125
* @param useConstructor Attempt to use constructor when creating mock instance
126
* @param outerInstance For mocking non-static inner classes with useConstructor
127
* @param lenient Bypass strict stubbing validation
128
* @returns MockSettings instance for use with Mockito.mock()
129
*/
130
fun withSettings(
131
extraInterfaces: Array<out KClass<out Any>>? = null,
132
name: String? = null,
133
spiedInstance: Any? = null,
134
defaultAnswer: Answer<Any>? = null,
135
serializable: Boolean = false,
136
serializableMode: SerializableMode? = null,
137
verboseLogging: Boolean = false,
138
invocationListeners: Array<InvocationListener>? = null,
139
stubOnly: Boolean = false,
140
useConstructor: UseConstructor? = null,
141
outerInstance: Any? = null,
142
lenient: Boolean = false
143
): MockSettings
144
```
145
146
**Usage Examples:**
147
148
```kotlin
149
import com.nhaarman.mockitokotlin2.*
150
import org.mockito.Mockito
151
152
// Using withSettings for advanced configuration
153
val settings = withSettings(
154
name = "advancedMock",
155
verboseLogging = true,
156
serializable = true
157
)
158
val mock = Mockito.mock(UserService::class.java, settings)
159
```
160
161
### Constructor-Based Mocking
162
163
Helper class for constructor-based mock creation.
164
165
```kotlin { .api }
166
/**
167
* Helper class for constructor-based mock creation
168
*/
169
class UseConstructor private constructor(val args: Array<Any>) {
170
companion object {
171
/** Creates UseConstructor for parameterless constructor invocation */
172
fun parameterless(): UseConstructor
173
174
/** Creates UseConstructor for constructor invocation with specified arguments */
175
fun withArguments(vararg arguments: Any): UseConstructor
176
}
177
}
178
```
179
180
**Usage Examples:**
181
182
```kotlin
183
import com.nhaarman.mockitokotlin2.*
184
185
// Mock using parameterless constructor
186
val mock = mock<SomeClass>(useConstructor = UseConstructor.parameterless())
187
188
// Mock using constructor with arguments
189
val mockWithArgs = mock<SomeClass>(
190
useConstructor = UseConstructor.withArguments("arg1", 42, true)
191
)
192
```
193
194
## Types
195
196
```kotlin { .api }
197
// From org.mockito
198
class MockSettings
199
class Answer<T>
200
class SerializableMode
201
class InvocationListener
202
class KClass<T>
203
204
// From mockito-kotlin
205
class UseConstructor private constructor(val args: Array<Any>) {
206
companion object {
207
fun parameterless(): UseConstructor
208
fun withArguments(vararg arguments: Any): UseConstructor
209
}
210
}
211
212
class KStubbing<out T>(val mock: T)
213
```