Migrate all remaining mockito tests to mockk.

Resolves #13835
This commit is contained in:
Jameson Williams
2024-12-10 21:19:10 -06:00
committed by Greyson Parrelli
parent 57eeed33f0
commit 34a003c68c
11 changed files with 272 additions and 263 deletions

View File

@@ -1,48 +0,0 @@
package org.thoughtcrime.securesms.recipients
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.MockedConstruction
import org.mockito.MockedStatic
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.keyvalue.SignalStore
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, application = Application::class)
abstract class BaseRecipientTest {
@Rule
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@Mock
private lateinit var applicationDependenciesStaticMock: MockedStatic<AppDependencies>
@Mock
private lateinit var attachmentSecretProviderStaticMock: MockedStatic<AttachmentSecretProvider>
@Mock
private lateinit var signalStoreStaticMock: MockedStatic<SignalStore>
@Mock
private lateinit var mockedSignalStoreConstruction: MockedConstruction<SignalStore>
@Before
fun superSetUp() {
val application = ApplicationProvider.getApplicationContext<Application>()
`when`(AppDependencies.application).thenReturn(application)
`when`(AttachmentSecretProvider.getInstance(ArgumentMatchers.any())).thenThrow(RuntimeException::class.java)
}
}

View File

@@ -1,25 +1,34 @@
package org.thoughtcrime.securesms.recipients
import android.app.Application
import android.graphics.Color
import androidx.test.core.app.ApplicationProvider
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.conversation.colors.ChatColors
import org.thoughtcrime.securesms.conversation.colors.ChatColorsPalette
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
import org.thoughtcrime.securesms.database.RecipientDatabaseTestUtils.createRecipient
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.keyvalue.ChatColorsValues
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.keyvalue.WallpaperValues
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper
@Suppress("ClassName")
class Recipient_getChatColorsTest : BaseRecipientTest() {
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, application = Application::class)
class Recipient_getChatColorsTest {
private val defaultChatColors = ChatColorsPalette.Bubbles.default.withId(ChatColors.Id.Auto)
private val globalWallpaperChatColor = ChatColors.forColor(ChatColors.Id.BuiltIn, Color.RED)
private val globalChatColor = ChatColors.forColor(ChatColors.Id.BuiltIn, Color.GREEN)
@@ -29,6 +38,12 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Before
fun setUp() {
mockkStatic(AppDependencies::class)
every { AppDependencies.application } returns ApplicationProvider.getApplicationContext()
mockkStatic(AttachmentSecretProvider::class)
every { AttachmentSecretProvider.getInstance(any()) } throws RuntimeException("Whoa, nelly!")
wallpaperValues = mockk()
chatColorsValues = mockk()
@@ -225,7 +240,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
private fun createWallpaper(
chatColors: ChatColors
): ChatWallpaper {
return mockk<ChatWallpaper>().apply {
return mockk<ChatWallpaper> {
every { autoChatColors } answers { chatColors }
}
}