Skip to content

Commit

Permalink
Support lenient in mock and withSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Sep 8, 2019
1 parent 442d227 commit 8baf2db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ import kotlin.reflect.KClass
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
* @param lenient Lenient mocks bypass "strict stubbing" validation.
*/
inline fun <reified T : Any> mock(
extraInterfaces: Array<KClass<out Any>>? = null,
extraInterfaces: Array<out KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
Expand All @@ -60,7 +61,8 @@ inline fun <reified T : Any> mock(
invocationListeners: Array<InvocationListener>? = null,
stubOnly: Boolean = false,
@Incubating useConstructor: UseConstructor? = null,
@Incubating outerInstance: Any? = null
@Incubating outerInstance: Any? = null,
@Incubating lenient: Boolean = false
): T {
return Mockito.mock(
T::class.java,
Expand All @@ -75,7 +77,8 @@ inline fun <reified T : Any> mock(
invocationListeners = invocationListeners,
stubOnly = stubOnly,
useConstructor = useConstructor,
outerInstance = outerInstance
outerInstance = outerInstance,
lenient = lenient
)
)!!
}
Expand All @@ -94,9 +97,10 @@ inline fun <reified T : Any> mock(
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
* @param lenient Lenient mocks bypass "strict stubbing" validation.
*/
inline fun <reified T : Any> mock(
extraInterfaces: Array<KClass<out Any>>? = null,
extraInterfaces: Array<out KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
Expand All @@ -107,6 +111,7 @@ inline fun <reified T : Any> mock(
stubOnly: Boolean = false,
@Incubating useConstructor: UseConstructor? = null,
@Incubating outerInstance: Any? = null,
@Incubating lenient: Boolean = false,
stubbing: KStubbing<T>.(T) -> Unit
): T {
return Mockito.mock(
Expand All @@ -122,7 +127,8 @@ inline fun <reified T : Any> mock(
invocationListeners = invocationListeners,
stubOnly = stubOnly,
useConstructor = useConstructor,
outerInstance = outerInstance
outerInstance = outerInstance,
lenient = lenient
)
).apply { KStubbing(this).stubbing(this) }!!
}
Expand All @@ -142,9 +148,10 @@ inline fun <reified T : Any> mock(
* @param stubOnly A stub-only mock does not record method invocations, thus saving memory but disallowing verification of invocations.
* @param useConstructor Mockito attempts to use constructor when creating instance of the mock.
* @param outerInstance Makes it possible to mock non-static inner classes in conjunction with [useConstructor].
* @param lenient Lenient mocks bypass "strict stubbing" validation.
*/
fun withSettings(
extraInterfaces: Array<KClass<out Any>>? = null,
extraInterfaces: Array<out KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
Expand All @@ -154,7 +161,8 @@ fun withSettings(
invocationListeners: Array<InvocationListener>? = null,
stubOnly: Boolean = false,
@Incubating useConstructor: UseConstructor? = null,
@Incubating outerInstance: Any? = null
@Incubating outerInstance: Any? = null,
@Incubating lenient: Boolean = false
): MockSettings = Mockito.withSettings().apply {
extraInterfaces?.let { extraInterfaces(*it.map { it.java }.toTypedArray()) }
name?.let { name(it) }
Expand All @@ -167,6 +175,7 @@ fun withSettings(
if (stubOnly) stubOnly()
useConstructor?.let { useConstructor(*it.args) }
outerInstance?.let { outerInstance(it) }
if (lenient) lenient()
}

class UseConstructor private constructor(val args: Array<Any>) {
Expand Down
20 changes: 18 additions & 2 deletions tests/src/test/kotlin/test/MockingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package test
import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.expect.fail
import com.nhaarman.mockitokotlin2.*
import com.nhaarman.mockitokotlin2.UseConstructor.Companion
import com.nhaarman.mockitokotlin2.UseConstructor.Companion.parameterless
import com.nhaarman.mockitokotlin2.UseConstructor.Companion.withArguments
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Test
import org.mockito.Mockito
import org.mockito.exceptions.verification.WantedButNotInvoked
Expand Down Expand Up @@ -255,6 +257,20 @@ class MockingTest : TestBase() {
}
}

@Test
fun mockStubbing_withSettingsAPIAndStubbing_name() {
/* Given */
val mock = mock<Methods>(name = "myName") {
on { nullableStringResult() } doReturn "foo"
}

/* When */
val result = mock.nullableStringResult()

/* Then */
expect(result).toBe("foo")
}

@Test
fun mockStubbing_withSettingsAPI_defaultAnswer() {
/* Given */
Expand Down

0 comments on commit 8baf2db

Please sign in to comment.