Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f74d8b84 authored by Mark Renouf's avatar Mark Renouf Committed by Android (Google) Code Review
Browse files

Merge "Deprecate KotlinMockitoHelpers in favor of org.mockito.kotlin" into main

parents 70dff58e 6048b422
Loading
Loading
Loading
Loading
+96 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ package com.android.systemui.util.mockito
 * be null"). To fix this, we can use methods that modify the return type to be nullable. This
 * causes Kotlin to skip the null checks.
 */
import kotlin.DeprecationLevel.WARNING
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatcher
import org.mockito.MockSettings
@@ -37,6 +38,11 @@ import org.mockito.stubbing.Stubber
 *
 * Generic T is nullable because implicitly bounded by Any?.
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "eq", imports = ["org.mockito.kotlin.eq"]),
    level = WARNING
)
fun <T> eq(obj: T): T = Mockito.eq<T>(obj) ?: obj

/**
@@ -45,8 +51,18 @@ fun <T> eq(obj: T): T = Mockito.eq<T>(obj) ?: obj
 *
 * Generic T is nullable because implicitly bounded by Any?.
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "any(type)", imports = ["org.mockito.kotlin.any"]),
    level = WARNING
)
fun <T> any(type: Class<T>): T = Mockito.any<T>(type)

@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "any()", imports = ["org.mockito.kotlin.any"]),
    level = WARNING
)
inline fun <reified T> any(): T = any(T::class.java)

/**
@@ -55,9 +71,23 @@ inline fun <reified T> any(): T = any(T::class.java)
 *
 * Generic T is nullable because implicitly bounded by Any?.
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "argThat(matcher)", imports = ["org.mockito.kotlin.argThat"]),
    level = WARNING
)
fun <T> argThat(matcher: ArgumentMatcher<T>): T = Mockito.argThat(matcher)

/** Kotlin type-inferred version of Mockito.nullable() */
/**
 * Kotlin type-inferred version of Mockito.nullable()
 *
 * @see org.mockito.kotlin.anyOrNull
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "anyOrNull()", imports = ["org.mockito.kotlin.anyOrNull"]),
    level = WARNING
)
inline fun <reified T> nullable(): T? = Mockito.nullable(T::class.java)

/**
@@ -65,14 +95,28 @@ inline fun <reified T> nullable(): T? = Mockito.nullable(T::class.java)
 * null is returned.
 *
 * Generic T is nullable because implicitly bounded by Any?.
 *
 * @see org.mockito.kotlin.capture
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "capture(argumentCaptor)", imports = ["org.mockito.kotlin.capture"]),
    level = WARNING
)
fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()

/**
 * Helper function for creating an argumentCaptor in kotlin.
 *
 * Generic T is nullable because implicitly bounded by Any?.
 *
 * @see org.mockito.kotlin.argumentCaptor
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "argumentCaptor()", imports = ["org.mockito.kotlin.argumentCaptor"]),
    level = WARNING
)
inline fun <reified T : Any> argumentCaptor(): ArgumentCaptor<T> =
    ArgumentCaptor.forClass(T::class.java)

@@ -81,18 +125,55 @@ inline fun <reified T : Any> argumentCaptor(): ArgumentCaptor<T> =
 *
 * Generic T is nullable because implicitly bounded by Any?.
 *
 * Updated kotlin-mockito usage:
 * ```
 * val value: Widget = mock<> {
 *    on { status } doReturn "OK"
 *    on { buttonPress } doNothing
 *    on { destroy } doAnswer error("Boom!")
 * }
 * ```
 *
 * __Deprecation note__
 *
 * Automatic replacement is not possible due to a change in lambda receiver type to KStubbing<T>
 *
 * @param apply builder function to simplify stub configuration by improving type inference.
 * @see org.mockito.kotlin.mock
 * @see org.mockito.kotlin.KStubbing.on
 */
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Replace with mockito-kotlin. See http://go/mockito-kotlin", level = WARNING)
inline fun <reified T : Any> mock(settings: MockSettings? = null, apply: T.() -> Unit = {}): T =
    Mockito.mock(T::class.java, settings ?: withSettings()).apply(apply)

/**
 * Helper function for stubbing methods without the need to use backticks.
 *
 * @see Mockito.when
 * Avoid. It is preferable to provide stubbing at creation time using the [mock] lambda argument.
 *
 * @see org.mockito.kotlin.whenever
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "whenever(methodCall)", imports = ["org.mockito.kotlin.whenever"]),
    level = WARNING
)
fun <T> whenever(methodCall: T): OngoingStubbing<T> = `when`(methodCall)

/**
 * __Deprecation note__
 *
 * Replace with `KStubbing<T>.on` within [org.mockito.kotlin.mock] { stubbing }
 *
 * @see org.mockito.kotlin.mock
 * @see org.mockito.kotlin.KStubbing.on
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "whenever(mock)", imports = ["org.mockito.kotlin.whenever"]),
    level = WARNING
)
fun <T> Stubber.whenever(mock: T): T = `when`(mock)

/**
@@ -101,6 +182,7 @@ fun <T> Stubber.whenever(mock: T): T = `when`(mock)
 *
 *     java.lang.NullPointerException: capture() must not be null
 */
@Deprecated("Replace with mockito-kotlin. See http://go/mockito-kotlin", level = WARNING)
class KotlinArgumentCaptor<T> constructor(clazz: Class<T>) {
    private val wrapped: ArgumentCaptor<T> = ArgumentCaptor.forClass(clazz)
    fun capture(): T = wrapped.capture()
@@ -114,7 +196,14 @@ class KotlinArgumentCaptor<T> constructor(clazz: Class<T>) {
 * Helper function for creating an argumentCaptor in kotlin.
 *
 * Generic T is nullable because implicitly bounded by Any?.
 *
 * @see org.mockito.kotlin.argumentCaptor
 */
@Deprecated(
    "Replace with mockito-kotlin. See http://go/mockito-kotlin",
    ReplaceWith(expression = "argumentCaptor()", imports = ["org.mockito.kotlin.argumentCaptor"]),
    level = WARNING
)
inline fun <reified T : Any> kotlinArgumentCaptor(): KotlinArgumentCaptor<T> =
    KotlinArgumentCaptor(T::class.java)

@@ -130,6 +219,7 @@ inline fun <reified T : Any> kotlinArgumentCaptor(): KotlinArgumentCaptor<T> =
 *
 * NOTE: this uses the KotlinArgumentCaptor to avoid the NullPointerException.
 */
@Deprecated("Replace with mockito-kotlin", level = WARNING)
inline fun <reified T : Any> withArgCaptor(block: KotlinArgumentCaptor<T>.() -> Unit): T =
    kotlinArgumentCaptor<T>().apply { block() }.value

@@ -142,6 +232,10 @@ inline fun <reified T : Any> withArgCaptor(block: KotlinArgumentCaptor<T>.() ->
 * becomes:
 *
 * val capturedList = captureMany<Foo> { verify(...).someMethod(capture()) }
 *
 * @see org.mockito.kotlin.verify
 */
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Replace with mockito-kotlin. See http://go/mockito-kotlin", level = WARNING)
inline fun <reified T : Any> captureMany(block: KotlinArgumentCaptor<T>.() -> Unit): List<T> =
    kotlinArgumentCaptor<T>().apply { block() }.allValues