|
26 | 26 | package com.nhaarman.mockito_kotlin
|
27 | 27 |
|
28 | 28 | import com.nhaarman.mockito_kotlin.createinstance.createInstance
|
29 |
| -import org.mockito.InOrder |
30 |
| -import org.mockito.MockSettings |
31 |
| -import org.mockito.MockingDetails |
32 |
| -import org.mockito.Mockito |
| 29 | +import org.mockito.* |
33 | 30 | import org.mockito.invocation.InvocationOnMock
|
| 31 | +import org.mockito.listeners.InvocationListener |
| 32 | +import org.mockito.mock.SerializableMode |
34 | 33 | import org.mockito.stubbing.Answer
|
35 | 34 | import org.mockito.stubbing.OngoingStubbing
|
36 | 35 | import org.mockito.stubbing.Stubber
|
37 | 36 | import org.mockito.verification.VerificationMode
|
38 | 37 | import org.mockito.verification.VerificationWithTimeout
|
| 38 | +import kotlin.DeprecationLevel.WARNING |
39 | 39 | import kotlin.reflect.KClass
|
40 | 40 |
|
41 | 41 | fun after(millis: Long) = Mockito.after(millis)
|
@@ -74,7 +74,7 @@ inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) =
|
74 | 74 | *
|
75 | 75 | * @param predicate A function that returns `true` when given [T] matches the predicate.
|
76 | 76 | */
|
77 |
| -inline fun <reified T: Any> argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate) |
| 77 | +inline fun <reified T : Any> argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate) |
78 | 78 |
|
79 | 79 | /**
|
80 | 80 | * For usage with verification only.
|
@@ -120,16 +120,70 @@ inline fun <reified T : Any> isA(): T = Mockito.isA(T::class.java) ?: createInst
|
120 | 120 | fun <T : Any> isNotNull(): T? = Mockito.isNotNull()
|
121 | 121 | fun <T : Any> isNull(): T? = Mockito.isNull()
|
122 | 122 |
|
123 |
| -inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)!! |
124 |
| -inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(T::class.java, defaultAnswer)!! |
| 123 | +inline fun <reified T : Any> mock( |
| 124 | + extraInterfaces: Array<KClass<out Any>>? = null, |
| 125 | + name: String? = null, |
| 126 | + spiedInstance: Any? = null, |
| 127 | + defaultAnswer: Answer<Any>? = null, |
| 128 | + serializable: Boolean = false, |
| 129 | + serializableMode: SerializableMode? = null, |
| 130 | + verboseLogging: Boolean = false, |
| 131 | + invocationListeners: Array<InvocationListener>? = null, |
| 132 | + stubOnly: Boolean = false, |
| 133 | + @Incubating useConstructor: Boolean = false, |
| 134 | + @Incubating outerInstance: Any? = null |
| 135 | +): T = Mockito.mock(T::class.java, withSettings( |
| 136 | + extraInterfaces = extraInterfaces, |
| 137 | + name = name, |
| 138 | + spiedInstance = spiedInstance, |
| 139 | + defaultAnswer = defaultAnswer, |
| 140 | + serializable = serializable, |
| 141 | + serializableMode = serializableMode, |
| 142 | + verboseLogging = verboseLogging, |
| 143 | + invocationListeners = invocationListeners, |
| 144 | + stubOnly = stubOnly, |
| 145 | + useConstructor = useConstructor, |
| 146 | + outerInstance = outerInstance |
| 147 | +))!! |
| 148 | + |
| 149 | +inline fun <reified T : Any> mock( |
| 150 | + extraInterfaces: Array<KClass<out Any>>? = null, |
| 151 | + name: String? = null, |
| 152 | + spiedInstance: Any? = null, |
| 153 | + defaultAnswer: Answer<Any>? = null, |
| 154 | + serializable: Boolean = false, |
| 155 | + serializableMode: SerializableMode? = null, |
| 156 | + verboseLogging: Boolean = false, |
| 157 | + invocationListeners: Array<InvocationListener>? = null, |
| 158 | + stubOnly: Boolean = false, |
| 159 | + @Incubating useConstructor: Boolean = false, |
| 160 | + @Incubating outerInstance: Any? = null, |
| 161 | + stubbing: KStubbing<T>.(T) -> Unit |
| 162 | +): T = Mockito.mock(T::class.java, withSettings( |
| 163 | + extraInterfaces = extraInterfaces, |
| 164 | + name = name, |
| 165 | + spiedInstance = spiedInstance, |
| 166 | + defaultAnswer = defaultAnswer, |
| 167 | + serializable = serializable, |
| 168 | + serializableMode = serializableMode, |
| 169 | + verboseLogging = verboseLogging, |
| 170 | + invocationListeners = invocationListeners, |
| 171 | + stubOnly = stubOnly, |
| 172 | + useConstructor = useConstructor, |
| 173 | + outerInstance = outerInstance |
| 174 | +)).apply { |
| 175 | + KStubbing(this).stubbing(this) |
| 176 | +}!! |
| 177 | + |
| 178 | +@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock<T>(defaultAnswer = a)"), level = WARNING) |
| 179 | +inline fun <reified T : Any> mock(a: Answer<Any>): T = mock(defaultAnswer = a) |
| 180 | + |
| 181 | +@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock<T>(name = s)"), level = WARNING) |
| 182 | +inline fun <reified T : Any> mock(s: String): T = mock(name = s) |
| 183 | + |
| 184 | +@Suppress("DeprecatedCallableAddReplaceWith") |
| 185 | +@Deprecated("Use mock() with optional arguments instead.", level = WARNING) |
125 | 186 | inline fun <reified T : Any> mock(s: MockSettings): T = Mockito.mock(T::class.java, s)!!
|
126 |
| -inline fun <reified T : Any> mock(s: String): T = Mockito.mock(T::class.java, s)!! |
127 |
| - |
128 |
| -inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T { |
129 |
| - return mock<T>().apply { |
130 |
| - KStubbing(this).stubbing(this) |
131 |
| - } |
132 |
| -} |
133 | 187 |
|
134 | 188 | class KStubbing<out T>(private val mock: T) {
|
135 | 189 | fun <R> on(methodCall: R) = Mockito.`when`(methodCall)
|
@@ -193,4 +247,29 @@ fun <T> verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteract
|
193 | 247 | fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks)
|
194 | 248 |
|
195 | 249 | fun <T> whenever(methodCall: T): OngoingStubbing<T> = Mockito.`when`(methodCall)!!
|
196 |
| -fun withSettings(): MockSettings = Mockito.withSettings()!! |
| 250 | + |
| 251 | +fun withSettings( |
| 252 | + extraInterfaces: Array<KClass<out Any>>? = null, |
| 253 | + name: String? = null, |
| 254 | + spiedInstance: Any? = null, |
| 255 | + defaultAnswer: Answer<Any>? = null, |
| 256 | + serializable: Boolean = false, |
| 257 | + serializableMode: SerializableMode? = null, |
| 258 | + verboseLogging: Boolean = false, |
| 259 | + invocationListeners: Array<InvocationListener>? = null, |
| 260 | + stubOnly: Boolean = false, |
| 261 | + @Incubating useConstructor: Boolean = false, |
| 262 | + @Incubating outerInstance: Any? = null |
| 263 | +): MockSettings = Mockito.withSettings().apply { |
| 264 | + extraInterfaces?.let { extraInterfaces(*it.map { it.java }.toTypedArray()) } |
| 265 | + name?.let { name(it) } |
| 266 | + spiedInstance?.let { spiedInstance(it) } |
| 267 | + defaultAnswer?.let { defaultAnswer(it) } |
| 268 | + if (serializable) serializable() |
| 269 | + serializableMode?.let { serializable(it) } |
| 270 | + if (verboseLogging) verboseLogging() |
| 271 | + invocationListeners?.let { invocationListeners(*it) } |
| 272 | + if (stubOnly) stubOnly() |
| 273 | + if (useConstructor) useConstructor() |
| 274 | + outerInstance?.let { outerInstance(it) } |
| 275 | +} |
0 commit comments