summaryrefslogtreecommitdiff
path: root/tests/shared
diff options
context:
space:
mode:
author Mark Renouf <mrenouf@google.com> 2024-04-03 19:59:33 -0400
committer Mark Renouf <mrenouf@google.com> 2024-04-25 17:24:06 -0400
commit5466dfbe357066d181aa6a2aa126448fae406c4f (patch)
tree0cd684f72add712c80c73c7845bd4a47b97da5bd /tests/shared
parentb4b571d55554ce2d0c1120f920195d792d3cfccd (diff)
Deprecate MockitoKotlinHelpers
Test: atest IntentResolver-tests-unit Bug: N/A Flag: NONE test only change Change-Id: I19b98753ecfc445ff9e93fb8f75c19447b299820
Diffstat (limited to 'tests/shared')
-rw-r--r--tests/shared/src/com/android/intentresolver/FrameworkMocksKosmos.kt4
-rw-r--r--tests/shared/src/com/android/intentresolver/MockitoKotlinHelpers.kt159
-rw-r--r--tests/shared/src/com/android/intentresolver/contentpreview/payloadtoggle/domain/intent/PendingIntentSenderKosmos.kt2
-rw-r--r--tests/shared/src/com/android/intentresolver/platform/FakeUserManager.kt31
4 files changed, 154 insertions, 42 deletions
diff --git a/tests/shared/src/com/android/intentresolver/FrameworkMocksKosmos.kt b/tests/shared/src/com/android/intentresolver/FrameworkMocksKosmos.kt
index d578fb66..df3931c6 100644
--- a/tests/shared/src/com/android/intentresolver/FrameworkMocksKosmos.kt
+++ b/tests/shared/src/com/android/intentresolver/FrameworkMocksKosmos.kt
@@ -20,6 +20,6 @@ import android.content.ContentResolver
import android.content.pm.PackageManager
import com.android.systemui.kosmos.Kosmos
-var Kosmos.contentResolver by Kosmos.Fixture { mock<ContentResolver> {} }
+var Kosmos.contentResolver by Kosmos.Fixture { org.mockito.kotlin.mock<ContentResolver> {} }
var Kosmos.contentInterface by Kosmos.Fixture { contentResolver }
-var Kosmos.packageManager by Kosmos.Fixture { mock<PackageManager> {} }
+var Kosmos.packageManager by Kosmos.Fixture { org.mockito.kotlin.mock<PackageManager> {} }
diff --git a/tests/shared/src/com/android/intentresolver/MockitoKotlinHelpers.kt b/tests/shared/src/com/android/intentresolver/MockitoKotlinHelpers.kt
index b7b97d6f..40ee6325 100644
--- a/tests/shared/src/com/android/intentresolver/MockitoKotlinHelpers.kt
+++ b/tests/shared/src/com/android/intentresolver/MockitoKotlinHelpers.kt
@@ -18,13 +18,7 @@
package com.android.intentresolver
-/**
- * Kotlin versions of popular mockito methods that can return null in situations when Kotlin expects
- * a non-null value. Kotlin will throw an IllegalStateException when this takes place ("x must not
- * 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. Cloned from
- * frameworks/base/packages/SystemUI/tests/utils/src/com/android/systemui/util/mockito/KotlinMockitoHelpers.kt
- */
+import kotlin.DeprecationLevel.WARNING
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatcher
import org.mockito.ArgumentMatchers
@@ -34,12 +28,25 @@ import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
+/*
+ * Kotlin versions of popular mockito methods that can return null in situations when Kotlin expects
+ * a non-null value. Kotlin will throw an IllegalStateException when this takes place ("x must not
+ * 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. Cloned from
+ * frameworks/base/packages/SystemUI/tests/utils/src/com/android/systemui/util/mockito/KotlinMockitoHelpers.kt
+ */
+
/**
* Returns Mockito.eq() as nullable type to avoid java.lang.IllegalStateException when null is
* returned.
*
* 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
+)
inline fun <T> eq(obj: T): T = Mockito.eq<T>(obj) ?: obj
/**
@@ -48,6 +55,11 @@ inline 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 = "same(obj)", imports = ["org.mockito.kotlin.same"]),
+ level = WARNING
+)
inline fun <T> same(obj: T): T = Mockito.same<T>(obj) ?: obj
/**
@@ -56,8 +68,18 @@ inline fun <T> same(obj: T): T = Mockito.same<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
+)
inline 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)
/**
@@ -66,9 +88,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
+)
inline 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)
/**
@@ -76,14 +112,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
+)
inline 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)
@@ -92,24 +142,68 @@ inline fun <reified T : Any> argumentCaptor(): ArgumentCaptor<T> =
*
* Generic T is nullable because implicitly bounded by Any?.
*
- * @param apply builder function to simplify stub configuration by improving type inference.
+ * 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>
+ *
+ * @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(
mockSettings: MockSettings = Mockito.withSettings(),
apply: T.() -> Unit = {}
): T = Mockito.mock(T::class.java, mockSettings).apply(apply)
/** Matches any array of type T. */
+@Deprecated(
+ "Replace with mockito-kotlin. See http://go/mockito-kotlin",
+ ReplaceWith(expression = "anyArray()", imports = ["org.mockito.kotlin.anyArray"]),
+ level = WARNING
+)
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
/**
* 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
+)
inline fun <T> whenever(methodCall: T): OngoingStubbing<T> = Mockito.`when`(methodCall)
-/** Helper function for stubbing methods without the need to use backticks. */
+/**
+ * Helper function for stubbing methods without the need to use backticks.
+ *
+ * Avoid. It is preferable to provide stubbing at creation time using the [mock] lambda argument.
+ *
+ * __Deprecation note__
+ *
+ * Replace with KStubber<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
+)
inline fun <T> Stubber.whenever(mock: T): T = `when`(mock)
/**
@@ -118,6 +212,7 @@ inline 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()
@@ -131,7 +226,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)
@@ -146,7 +248,11 @@ inline fun <reified T : Any> kotlinArgumentCaptor(): KotlinArgumentCaptor<T> =
* val captured = withArgCaptor<Foo> { verify(...).someMethod(capture()) }
*
* NOTE: this uses the KotlinArgumentCaptor to avoid the NullPointerException.
+ *
+ * @see org.mockito.kotlin.verify
*/
+@Suppress("DeprecatedCallableAddReplaceWith")
+@Deprecated("Replace with mockito-kotlin. See http://go/mockito-kotlin", level = WARNING)
inline fun <reified T : Any> withArgCaptor(block: KotlinArgumentCaptor<T>.() -> Unit): T =
kotlinArgumentCaptor<T>().apply { block() }.value
@@ -159,27 +265,28 @@ inline fun <reified T : Any> withArgCaptor(block: KotlinArgumentCaptor<T>.() ->
* becomes:
*
* val capturedList = captureMany<Foo> { verify(...).someMethod(capture()) }
+ *
+ * @see org.mockito.kotlin.verify
*/
+@Deprecated(
+ "Replace with mockito-kotlin. See http://go/mockito-kotlin",
+ ReplaceWith(expression = "capture()", imports = ["org.mockito.kotlin.capture"]),
+ level = WARNING
+)
inline fun <reified T : Any> captureMany(block: KotlinArgumentCaptor<T>.() -> Unit): List<T> =
kotlinArgumentCaptor<T>().apply { block() }.allValues
+/** @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> anyOrNull() = ArgumentMatchers.argThat(ArgumentMatcher<T?> { true })
/**
- * Intended as a default Answer for a mock to prevent dependence on defaults.
- *
- * Use as:
- * ```
- * val context = mock<Context>(withSettings()
- * .defaultAnswer(THROWS_EXCEPTION))
- * ```
- *
- * To avoid triggering the exception during stubbing, must ONLY use one of the doXXX() methods, such
- * as:
- * * [doAnswer][Mockito.doAnswer]
- * * [doCallRealMethod][Mockito.doCallRealMethod]
- * * [doNothing][Mockito.doNothing]
- * * [doReturn][Mockito.doReturn]
- * * [doThrow][Mockito.doThrow]
+ * @see org.mockito.kotlin.mock
+ * @see org.mockito.kotlin.doThrow
*/
+@Deprecated("Replace with mockito-kotlin. See http://go/mockito-kotlin", level = WARNING)
val THROWS_EXCEPTION = Answer { error("Unstubbed behavior was accessed.") }
diff --git a/tests/shared/src/com/android/intentresolver/contentpreview/payloadtoggle/domain/intent/PendingIntentSenderKosmos.kt b/tests/shared/src/com/android/intentresolver/contentpreview/payloadtoggle/domain/intent/PendingIntentSenderKosmos.kt
index f022373d..1b4c0c8f 100644
--- a/tests/shared/src/com/android/intentresolver/contentpreview/payloadtoggle/domain/intent/PendingIntentSenderKosmos.kt
+++ b/tests/shared/src/com/android/intentresolver/contentpreview/payloadtoggle/domain/intent/PendingIntentSenderKosmos.kt
@@ -16,7 +16,7 @@
package com.android.intentresolver.contentpreview.payloadtoggle.domain.intent
-import com.android.intentresolver.mock
import com.android.systemui.kosmos.Kosmos
+import org.mockito.kotlin.mock
var Kosmos.pendingIntentSender by Kosmos.Fixture { mock<PendingIntentSender> {} }
diff --git a/tests/shared/src/com/android/intentresolver/platform/FakeUserManager.kt b/tests/shared/src/com/android/intentresolver/platform/FakeUserManager.kt
index ff1e84bd..32cb9062 100644
--- a/tests/shared/src/com/android/intentresolver/platform/FakeUserManager.kt
+++ b/tests/shared/src/com/android/intentresolver/platform/FakeUserManager.kt
@@ -26,22 +26,18 @@ import android.os.IUserManager
import android.os.UserHandle
import android.os.UserManager
import androidx.annotation.NonNull
-import com.android.intentresolver.THROWS_EXCEPTION
import com.android.intentresolver.data.repository.AvailabilityChange
import com.android.intentresolver.data.repository.ProfileAdded
import com.android.intentresolver.data.repository.ProfileRemoved
import com.android.intentresolver.data.repository.UserEvent
-import com.android.intentresolver.mock
import com.android.intentresolver.platform.FakeUserManager.State
-import com.android.intentresolver.whenever
import kotlin.random.Random
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.consumeAsFlow
-import org.mockito.Mockito.RETURNS_SELF
-import org.mockito.Mockito.doAnswer
-import org.mockito.Mockito.doReturn
-import org.mockito.Mockito.withSettings
+import org.mockito.kotlin.doReturn
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
/**
* A stand-in for [UserManager] to support testing of data layer components which depend on it.
@@ -211,11 +207,16 @@ class FakeUserManager(val state: State = State()) :
}
/** A safe mock of [Context] which throws on any unstubbed method call. */
-private fun mockContext(user: UserHandle = UserHandle.SYSTEM): Context {
- return mock<Context>(withSettings().defaultAnswer(THROWS_EXCEPTION)) {
- doAnswer(RETURNS_SELF).whenever(this).applicationContext
- doReturn(user).whenever(this).user
- doReturn(user.identifier).whenever(this).userId
+private fun mockContext(userHandle: UserHandle = UserHandle.SYSTEM): Context {
+ return mock<Context>(
+ defaultAnswer = {
+ error("Unstubbed behavior invoked! (${it.method}(${it.arguments.asList()})")
+ }
+ ) {
+ // Careful! Specify behaviors *first* to avoid throwing while stubbing!
+ doReturn(mock).whenever(mock).applicationContext
+ doReturn(userHandle).whenever(mock).user
+ doReturn(userHandle.identifier).whenever(mock).userId
}
}
@@ -229,7 +230,11 @@ private fun FakeUserManager.ProfileType.toUserType(): String {
/** A safe mock of [IUserManager] which throws on any unstubbed method call. */
fun mockService(): IUserManager {
- return mock<IUserManager>(withSettings().defaultAnswer(THROWS_EXCEPTION))
+ return mock<IUserManager>(
+ defaultAnswer = {
+ error("Unstubbed behavior invoked! ${it.method}(${it.arguments.asList()}")
+ }
+ )
}
val UserInfo.debugString: String