2525
2626package org.mockito.kotlin
2727
28- import org.mockito.Incubating
2928import org.mockito.MockSettings
3029import org.mockito.MockedConstruction
3130import org.mockito.MockedStatic
@@ -51,7 +50,8 @@ import kotlin.reflect.KClass
5150 * @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
5251 * @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
5352 * @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
54- * @param lenient Lenient mocks bypass "strict stubbing" validation.
53+ * @param lenient (DEPRECATED) Lenient mocks bypass "strict stubbing" validation.
54+ * @param strictness Specifies strictness level for the mock.
5555 */
5656inline fun <reified T : Any > mock (
5757 extraInterfaces : Array <out KClass <out Any >>? = null,
@@ -63,9 +63,10 @@ inline fun <reified T : Any> mock(
6363 verboseLogging : Boolean = false,
6464 invocationListeners : Array <InvocationListener >? = null,
6565 stubOnly : Boolean = false,
66- @Incubating useConstructor : UseConstructor ? = null,
67- @Incubating outerInstance : Any? = null,
68- @Incubating lenient : Boolean = false
66+ useConstructor : UseConstructor ? = null,
67+ outerInstance : Any? = null,
68+ lenient : Boolean = false,
69+ strictness : Strictness ? = if (lenient) Strictness .LENIENT else null,
6970): T {
7071 return Mockito .mock(
7172 T ::class .java,
@@ -81,7 +82,7 @@ inline fun <reified T : Any> mock(
8182 stubOnly = stubOnly,
8283 useConstructor = useConstructor,
8384 outerInstance = outerInstance,
84- lenient = lenient
85+ strictness = strictness
8586 )
8687 )!!
8788}
@@ -100,7 +101,8 @@ inline fun <reified T : Any> mock(
100101 * @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
101102 * @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
102103 * @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
103- * @param lenient Lenient mocks bypass "strict stubbing" validation.
104+ * @param lenient (DEPRECATED) Lenient mocks bypass "strict stubbing" validation.
105+ * @param strictness Specifies strictness level for the mock.
104106 */
105107inline fun <reified T : Any > mock (
106108 extraInterfaces : Array <out KClass <out Any >>? = null,
@@ -112,9 +114,10 @@ inline fun <reified T : Any> mock(
112114 verboseLogging : Boolean = false,
113115 invocationListeners : Array <InvocationListener >? = null,
114116 stubOnly : Boolean = false,
115- @Incubating useConstructor : UseConstructor ? = null,
116- @Incubating outerInstance : Any? = null,
117- @Incubating lenient : Boolean = false,
117+ useConstructor : UseConstructor ? = null,
118+ outerInstance : Any? = null,
119+ lenient : Boolean = false,
120+ strictness : Strictness ? = if (lenient) Strictness .LENIENT else null,
118121 stubbing : KStubbing <T >.(T ) -> Unit
119122): T {
120123 return Mockito .mock(
@@ -131,7 +134,7 @@ inline fun <reified T : Any> mock(
131134 stubOnly = stubOnly,
132135 useConstructor = useConstructor,
133136 outerInstance = outerInstance,
134- lenient = lenient
137+ strictness = strictness,
135138 )
136139 ).apply { KStubbing (this ).stubbing(this ) }!!
137140}
@@ -151,7 +154,8 @@ inline fun <reified T : Any> mock(
151154 * @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
152155 * @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
153156 * @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
154- * @param lenient Lenient mocks bypass "strict stubbing" validation.
157+ * @param lenient (DEPRECATED) Lenient mocks bypass "strict stubbing" validation.
158+ * @param strictness Specifies strictness level for the mock.
155159 */
156160fun withSettings (
157161 extraInterfaces : Array <out KClass <out Any >>? = null,
@@ -163,9 +167,10 @@ fun withSettings(
163167 verboseLogging : Boolean = false,
164168 invocationListeners : Array <InvocationListener >? = null,
165169 stubOnly : Boolean = false,
166- @Incubating useConstructor : UseConstructor ? = null,
167- @Incubating outerInstance : Any? = null,
168- @Incubating lenient : Boolean = false
170+ useConstructor : UseConstructor ? = null,
171+ outerInstance : Any? = null,
172+ lenient : Boolean = false,
173+ strictness : Strictness ? = if (lenient) Strictness .LENIENT else null,
169174): MockSettings = Mockito .withSettings().apply {
170175 extraInterfaces?.let { extraInterfaces(* it.map { it.java }.toTypedArray()) }
171176 name?.let { name(it) }
@@ -178,16 +183,19 @@ fun withSettings(
178183 if (stubOnly) stubOnly()
179184 useConstructor?.let { useConstructor(* it.args) }
180185 outerInstance?.let { outerInstance(it) }
181- if (lenient) strictness(Strictness . LENIENT )
186+ strictness?. let { strictness(it) }
182187}
183188
184189/* *
185190 * Creates a thread-local mock for static methods on [T].
186191 *
192+ * @param defaultAnswer the default answer when invoking static methods.
187193 * @see Mockito.mockStatic
188194 */
189- inline fun <reified T > mockStatic (): MockedStatic <T > {
190- return Mockito .mockStatic(T ::class .java)
195+ inline fun <reified T > mockStatic (
196+ defaultAnswer : Answer <Any >? = null,
197+ ): MockedStatic <T > {
198+ return Mockito .mockStatic(T ::class .java, withSettings(defaultAnswer = defaultAnswer))
191199}
192200
193201/* *
0 commit comments