Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lenient in mock and withSettings #353

Merged
merged 1 commit into from Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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