Skip to content

Commit 8baf2db

Browse files
committed
Support lenient in mock and withSettings
1 parent 442d227 commit 8baf2db

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/Mocking.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ import kotlin.reflect.KClass
4848
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
4949
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
5050
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
51+
* @param lenient Lenient mocks bypass "strict stubbing" validation.
5152
*/
5253
inline fun <reified T : Any> mock(
53-
extraInterfaces: Array<KClass<out Any>>? = null,
54+
extraInterfaces: Array<out KClass<out Any>>? = null,
5455
name: String? = null,
5556
spiedInstance: Any? = null,
5657
defaultAnswer: Answer<Any>? = null,
@@ -60,7 +61,8 @@ inline fun <reified T : Any> mock(
6061
invocationListeners: Array<InvocationListener>? = null,
6162
stubOnly: Boolean = false,
6263
@Incubating useConstructor: UseConstructor? = null,
63-
@Incubating outerInstance: Any? = null
64+
@Incubating outerInstance: Any? = null,
65+
@Incubating lenient: Boolean = false
6466
): T {
6567
return Mockito.mock(
6668
T::class.java,
@@ -75,7 +77,8 @@ inline fun <reified T : Any> mock(
7577
invocationListeners = invocationListeners,
7678
stubOnly = stubOnly,
7779
useConstructor = useConstructor,
78-
outerInstance = outerInstance
80+
outerInstance = outerInstance,
81+
lenient = lenient
7982
)
8083
)!!
8184
}
@@ -94,9 +97,10 @@ inline fun <reified T : Any> mock(
9497
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
9598
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
9699
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
100+
* @param lenient Lenient mocks bypass "strict stubbing" validation.
97101
*/
98102
inline fun <reified T : Any> mock(
99-
extraInterfaces: Array<KClass<out Any>>? = null,
103+
extraInterfaces: Array<out KClass<out Any>>? = null,
100104
name: String? = null,
101105
spiedInstance: Any? = null,
102106
defaultAnswer: Answer<Any>? = null,
@@ -107,6 +111,7 @@ inline fun <reified T : Any> mock(
107111
stubOnly: Boolean = false,
108112
@Incubating useConstructor: UseConstructor? = null,
109113
@Incubating outerInstance: Any? = null,
114+
@Incubating lenient: Boolean = false,
110115
stubbing: KStubbing<T>.(T) -> Unit
111116
): T {
112117
return Mockito.mock(
@@ -122,7 +127,8 @@ inline fun <reified T : Any> mock(
122127
invocationListeners = invocationListeners,
123128
stubOnly = stubOnly,
124129
useConstructor = useConstructor,
125-
outerInstance = outerInstance
130+
outerInstance = outerInstance,
131+
lenient = lenient
126132
)
127133
).apply { KStubbing(this).stubbing(this) }!!
128134
}
@@ -142,9 +148,10 @@ inline fun <reified T : Any> mock(
142148
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
143149
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
144150
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
151+
* @param lenient Lenient mocks bypass "strict stubbing" validation.
145152
*/
146153
fun withSettings(
147-
extraInterfaces: Array<KClass<out Any>>? = null,
154+
extraInterfaces: Array<out KClass<out Any>>? = null,
148155
name: String? = null,
149156
spiedInstance: Any? = null,
150157
defaultAnswer: Answer<Any>? = null,
@@ -154,7 +161,8 @@ fun withSettings(
154161
invocationListeners: Array<InvocationListener>? = null,
155162
stubOnly: Boolean = false,
156163
@Incubating useConstructor: UseConstructor? = null,
157-
@Incubating outerInstance: Any? = null
164+
@Incubating outerInstance: Any? = null,
165+
@Incubating lenient: Boolean = false
158166
): MockSettings = Mockito.withSettings().apply {
159167
extraInterfaces?.let { extraInterfaces(*it.map { it.java }.toTypedArray()) }
160168
name?.let { name(it) }
@@ -167,6 +175,7 @@ fun withSettings(
167175
if (stubOnly) stubOnly()
168176
useConstructor?.let { useConstructor(*it.args) }
169177
outerInstance?.let { outerInstance(it) }
178+
if (lenient) lenient()
170179
}
171180

172181
class UseConstructor private constructor(val args: Array<Any>) {

tests/src/test/kotlin/test/MockingTest.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package test
33
import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
55
import com.nhaarman.expect.fail
6-
import com.nhaarman.mockitokotlin2.*
7-
import com.nhaarman.mockitokotlin2.UseConstructor.Companion
86
import com.nhaarman.mockitokotlin2.UseConstructor.Companion.parameterless
97
import com.nhaarman.mockitokotlin2.UseConstructor.Companion.withArguments
8+
import com.nhaarman.mockitokotlin2.doReturn
9+
import com.nhaarman.mockitokotlin2.mock
10+
import com.nhaarman.mockitokotlin2.verify
11+
import com.nhaarman.mockitokotlin2.whenever
1012
import org.junit.Test
1113
import org.mockito.Mockito
1214
import org.mockito.exceptions.verification.WantedButNotInvoked
@@ -255,6 +257,20 @@ class MockingTest : TestBase() {
255257
}
256258
}
257259

260+
@Test
261+
fun mockStubbing_withSettingsAPIAndStubbing_name() {
262+
/* Given */
263+
val mock = mock<Methods>(name = "myName") {
264+
on { nullableStringResult() } doReturn "foo"
265+
}
266+
267+
/* When */
268+
val result = mock.nullableStringResult()
269+
270+
/* Then */
271+
expect(result).toBe("foo")
272+
}
273+
258274
@Test
259275
fun mockStubbing_withSettingsAPI_defaultAnswer() {
260276
/* Given */

0 commit comments

Comments
 (0)