Use Mockito Kotlin
Which has better support for Kotlin unit test.
Bug: 289155281
Test: unit test
Change-Id: Ic98ba7e66c124e8398ad01d3bca7ee3a771b06c4
diff --git a/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt
index 68ad065..ccd385f 100644
--- a/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt
+++ b/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt
@@ -31,9 +31,8 @@
import com.android.settings.R
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spa.testutils.FakeNavControllerWrapper
-import com.android.settingslib.spa.testutils.any
import com.android.settingslib.spaprivileged.template.app.AppListItemModel
-import com.google.common.truth.Truth
+import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
@@ -41,12 +40,13 @@
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
-import org.mockito.ArgumentCaptor
-import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
-import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
+import org.mockito.kotlin.any
+import org.mockito.kotlin.argumentCaptor
+import org.mockito.kotlin.eq
+import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class BackgroundInstalledAppsPageProviderTest {
@@ -67,26 +67,25 @@
@Mock
private lateinit var mockBackgroundInstallControlService: IBackgroundInstallControlService
- private var packageInfoFlagsCaptor =
- ArgumentCaptor.forClass(PackageManager.PackageInfoFlags::class.java)
+ private var packageInfoFlagsCaptor = argumentCaptor<PackageManager.PackageInfoFlags>()
private val fakeNavControllerWrapper = FakeNavControllerWrapper()
@Before
fun setup() {
- Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
+ whenever(mockContext.packageManager).thenReturn(mockPackageManager)
}
@Test
fun allAppListPageProvider_name() {
- Truth.assertThat(BackgroundInstalledAppsPageProvider.name)
+ assertThat(BackgroundInstalledAppsPageProvider.name)
.isEqualTo(EXPECTED_PROVIDER_NAME)
}
@Test
fun injectEntry_title() {
- Mockito.`when`(mockBackgroundInstallControlService
- .getBackgroundInstalledPackages(any(Long::class.java), any(Int::class.java)))
+ whenever(mockBackgroundInstallControlService.getBackgroundInstalledPackages(any(), any()))
.thenReturn(ParceledListSlice(listOf()))
+
setInjectEntry(false)
composeTestRule.onNodeWithText(
@@ -103,9 +102,9 @@
@Test
fun injectEntry_summary() {
- Mockito.`when`(mockBackgroundInstallControlService
- .getBackgroundInstalledPackages(any(Long::class.java), any(Int::class.java)))
+ whenever(mockBackgroundInstallControlService.getBackgroundInstalledPackages(any(), any()))
.thenReturn(ParceledListSlice(listOf()))
+
setInjectEntry(false)
composeTestRule.onNodeWithText("0 apps").assertIsDisplayed()
@@ -120,15 +119,14 @@
@Test
fun injectEntry_onClick_navigate() {
- Mockito.`when`(mockBackgroundInstallControlService
- .getBackgroundInstalledPackages(any(Long::class.java), any(Int::class.java)))
+ whenever(mockBackgroundInstallControlService.getBackgroundInstalledPackages(any(), any()))
.thenReturn(ParceledListSlice(listOf()))
setInjectEntry(false)
composeTestRule.onNodeWithText(
context.getString(R.string.background_install_title)).performClick()
- Truth.assertThat(fakeNavControllerWrapper.navigateCalledWith)
+ assertThat(fakeNavControllerWrapper.navigateCalledWith)
.isEqualTo(EXPECTED_PROVIDER_NAME)
}
@@ -166,7 +164,7 @@
composeTestRule.onNodeWithText(TEST_LABEL).performClick()
- Truth.assertThat(fakeNavControllerWrapper.navigateCalledWith)
+ assertThat(fakeNavControllerWrapper.navigateCalledWith)
.isEqualTo("AppInfoSettings/package.name/0")
}
@@ -181,7 +179,7 @@
System.currentTimeMillis()
))
- Truth.assertThat(actualGroupTitle).isEqualTo("Apps installed in the last 6 months")
+ assertThat(actualGroupTitle).isEqualTo("Apps installed in the last 6 months")
}
@Test
@@ -195,13 +193,13 @@
0L
))
- Truth.assertThat(actualGroupTitle).isEqualTo("Apps installed more than 6 months ago")
+ assertThat(actualGroupTitle).isEqualTo("Apps installed more than 6 months ago")
}
@Test
fun backgroundInstalledAppsWithGroupingListModel_transform() = runTest {
val listModel = BackgroundInstalledAppsWithGroupingListModel(mockContext)
- Mockito.`when`(mockPackageManager.getPackageInfoAsUser(
+ whenever(mockPackageManager.getPackageInfoAsUser(
eq(TEST_PACKAGE_NAME),
packageInfoFlagsCaptor.capture(),
eq(TEST_USER_ID))
@@ -211,16 +209,16 @@
val recordList = recordListFlow.first()
- Truth.assertThat(recordList).hasSize(1)
- Truth.assertThat(recordList[0].app).isSameInstanceAs(APP)
- Truth.assertThat(packageInfoFlagsCaptor.value.value).isEqualTo(EXPECTED_PACKAGE_INFO_FLAG)
+ assertThat(recordList).hasSize(1)
+ assertThat(recordList[0].app).isSameInstanceAs(APP)
+ assertThat(packageInfoFlagsCaptor.firstValue.value).isEqualTo(EXPECTED_PACKAGE_INFO_FLAG)
}
@Test
fun backgroundInstalledAppsWithGroupingListModel_filter() = runTest {
val listModel = BackgroundInstalledAppsWithGroupingListModel(mockContext)
listModel.setBackgroundInstallControlService(mockBackgroundInstallControlService)
- Mockito.`when`(mockBackgroundInstallControlService.getBackgroundInstalledPackages(
+ whenever(mockBackgroundInstallControlService.getBackgroundInstalledPackages(
PackageManager.MATCH_ALL.toLong(),
TEST_USER_ID
)).thenReturn(ParceledListSlice(listOf(PACKAGE_INFO)))
@@ -231,10 +229,9 @@
flowOf(listOf(APP_RECORD_WITH_PACKAGE_MATCH, APP_RECORD_WITHOUT_PACKAGE_MATCH))
)
-
val recordList = recordListFlow.first()
- Truth.assertThat(recordList).hasSize(1)
- Truth.assertThat(recordList[0]).isSameInstanceAs(APP_RECORD_WITH_PACKAGE_MATCH)
+ assertThat(recordList).hasSize(1)
+ assertThat(recordList[0]).isSameInstanceAs(APP_RECORD_WITH_PACKAGE_MATCH)
}
private fun setItemContent() {
diff --git a/tests/spa_unit/src/com/android/settings/spa/notification/AppNotificationRepositoryTest.kt b/tests/spa_unit/src/com/android/settings/spa/notification/AppNotificationRepositoryTest.kt
index 87ff9ba..5dcc671 100644
--- a/tests/spa_unit/src/com/android/settings/spa/notification/AppNotificationRepositoryTest.kt
+++ b/tests/spa_unit/src/com/android/settings/spa/notification/AppNotificationRepositoryTest.kt
@@ -30,7 +30,6 @@
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
-import com.android.settingslib.spa.testutils.any
import com.android.settingslib.spaprivileged.model.app.IPackageManagers
import com.android.settingslib.spaprivileged.model.app.userId
import com.google.common.truth.Truth.assertThat
@@ -42,11 +41,12 @@
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
-import org.mockito.Mockito.eq
-import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
-import org.mockito.Mockito.`when` as whenever
+import org.mockito.kotlin.any
+import org.mockito.kotlin.eq
+import org.mockito.kotlin.verify
+import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class AppNotificationRepositoryTest {