Skip to content

Commit

Permalink
Convert a batch of tests from mockito -> mockk.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesonwilliams authored and TheTechZone committed Jan 6, 2025
1 parent 1de7c46 commit 25e1982
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.thoughtcrime.securesms.sms

import android.app.Application
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.attachments.Attachment
Expand Down Expand Up @@ -36,18 +34,15 @@ import java.util.concurrent.atomic.AtomicLong
@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class UploadDependencyGraphTest {

private val jobManager: JobManager = mock()

private var uniqueLong = AtomicLong(0)

@Before
fun setUp() {
whenever(jobManager.startChain(any<Job>())).then {
JobManager.Chain(jobManager, listOf(it.getArgument(0)))
private val jobManager = mockk<JobManager> {
every { startChain(any<Job>()) } answers {
val job = args.first() as Job
JobManager.Chain(this@mockk, listOf(job))
}
}

private var uniqueLong = AtomicLong(0)

@Test
fun `Given a list of Uri attachments and a list of Messages, when I get the dependencyMap, then I expect a times m results`() {
// GIVEN
Expand Down
60 changes: 23 additions & 37 deletions app/src/test/java/org/thoughtcrime/securesms/stories/StoriesTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package org.thoughtcrime.securesms.stories

import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.slot
import io.mockk.verify
import io.reactivex.rxjava3.plugins.RxJavaPlugins
import io.reactivex.rxjava3.schedulers.TestScheduler
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.kotlin.any
import org.mockito.kotlin.isA
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.thoughtcrime.securesms.attachments.AttachmentId
import org.thoughtcrime.securesms.database.AttachmentTable
import org.thoughtcrime.securesms.database.FakeMessageRecords
Expand All @@ -24,37 +21,24 @@ import org.thoughtcrime.securesms.jobmanager.JobManager
import org.thoughtcrime.securesms.jobs.AttachmentDownloadJob

class StoriesTest {

@Rule
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule()

@Mock
private lateinit var mockAttachmentTable: AttachmentTable

@Mock
private lateinit var mockJobManager: JobManager

@Mock
private lateinit var mockApplicationDependenciesStatic: MockedStatic<AppDependencies>

@Mock
private lateinit var mockSignalDatabaseStatic: MockedStatic<SignalDatabase>

@Mock
private lateinit var mockSignalDatabase: SignalDatabase

private val testScheduler = TestScheduler()
private val mockJobManager = mockk<JobManager> {
every { add(any()) } just runs
}
private val mockAttachmentTable = mockk<AttachmentTable>()
private val mockSignalDatabase = mockk<SignalDatabase>()

@Before
fun setUp() {
mockkStatic(AppDependencies::class)

RxJavaPlugins.setInitIoSchedulerHandler { testScheduler }
RxJavaPlugins.setIoSchedulerHandler { testScheduler }

SignalDatabase.setSignalDatabaseInstanceForTesting(mockSignalDatabase)
whenever(SignalDatabase.attachments).thenReturn(mockAttachmentTable)
whenever(AppDependencies.jobManager).thenReturn(mockJobManager)
whenever(mockAttachmentTable.getAttachmentsForMessage(any())).thenReturn(emptyList())
every { SignalDatabase.attachments } returns mockAttachmentTable
every { AppDependencies.jobManager } returns mockJobManager
every { mockAttachmentTable.getAttachmentsForMessage(any()) } returns emptyList()
}

@After
Expand All @@ -75,7 +59,7 @@ class StoriesTest {

// THEN
testObserver.assertComplete()
verify(mockJobManager, never()).add(any())
verify(exactly = 0) { mockJobManager.add(any()) }
}

@Test
Expand All @@ -95,22 +79,24 @@ class StoriesTest {

// THEN
testObserver.assertComplete()
verify(mockJobManager).add(isA<AttachmentDownloadJob>())
val slot = slot<AttachmentDownloadJob>()
verify { mockJobManager.add(capture(slot)) }
}

@Test
fun `Given a MessageRecord with an attachment, when I enqueueAttachmentsFromStoryForDownload, then I enqueue once`() {
// GIVEN
val attachment = FakeMessageRecords.buildDatabaseAttachment()
val messageRecord = FakeMessageRecords.buildMediaMmsMessageRecord()
whenever(mockAttachmentTable.getAttachmentsForMessage(any())).thenReturn(listOf(attachment))
every { mockAttachmentTable.getAttachmentsForMessage(any()) } returns listOf(attachment)

// WHEN
val testObserver = Stories.enqueueAttachmentsFromStoryForDownload(messageRecord, true).test()
testScheduler.triggerActions()

// THEN
testObserver.assertComplete()
verify(mockJobManager).add(isA<AttachmentDownloadJob>())
val slot = slot<AttachmentDownloadJob>()
verify { mockJobManager.add(capture(slot)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,61 @@ import android.os.Looper.getMainLooper
import android.view.ContextThemeWrapper
import android.view.View
import android.widget.ImageView
import androidx.test.core.app.ApplicationProvider
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.verify
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.blurhash.BlurHash
import org.thoughtcrime.securesms.util.visible

@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class StoryFirstTimeNavigationViewTest {
private val testSubject =
StoryFirstTimeNavigationView(ContextThemeWrapper(getApplicationContext(), R.style.Signal_DayNight))

@Rule
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule()

private lateinit var testSubject: StoryFirstTimeNavigationView

@Mock
private lateinit var glide: MockedStatic<Glide>

@Mock
private lateinit var requestManager: RequestManager

@Mock
private lateinit var requestBuilder: RequestBuilder<Drawable>
private val requestBuilder = mockk<RequestBuilder<Drawable>>(relaxed = true) {
every { addListener(any()) } returns this@mockk
}
private val requestManager = mockk<RequestManager>(relaxUnitFun = true) {
every { load(any<BlurHash>()) } returns requestBuilder
}

@Before
fun setUp() {
testSubject = StoryFirstTimeNavigationView(ContextThemeWrapper(ApplicationProvider.getApplicationContext(), org.thoughtcrime.securesms.R.style.Signal_DayNight))

whenever(Glide.with(any<View>())).thenReturn(requestManager)
whenever(requestManager.load(any<BlurHash>())).thenReturn(requestBuilder)
whenever(requestBuilder.addListener(any())).thenReturn(requestBuilder)
mockkStatic(Glide::class)
every { Glide.with(any<View>()) } returns requestManager
}

@Test
@Config(sdk = [31])
fun `Given sdk 31, when I create testSubject, then I expect overlay visible and blur hash not visible`() {
shadowOf(getMainLooper()).idle()

assertTrue(testSubject.findViewById<View>(org.thoughtcrime.securesms.R.id.edu_overlay).visible)
assertFalse(testSubject.findViewById<View>(org.thoughtcrime.securesms.R.id.edu_blur_hash).visible)
assertTrue(testSubject.findViewById<View>(R.id.edu_overlay).visible)
assertFalse(testSubject.findViewById<View>(R.id.edu_blur_hash).visible)
}

@Test
@Config(sdk = [30])
fun `Given sdk 30, when I create testSubject, then I expect overlay visible and blur hash visible`() {
shadowOf(getMainLooper()).idle()

assertTrue(testSubject.findViewById<View>(org.thoughtcrime.securesms.R.id.edu_overlay).visible)
assertTrue(testSubject.findViewById<View>(org.thoughtcrime.securesms.R.id.edu_blur_hash).visible)
assertTrue(testSubject.findViewById<View>(R.id.edu_overlay).visible)
assertTrue(testSubject.findViewById<View>(R.id.edu_blur_hash).visible)
}

@Test
Expand All @@ -84,7 +70,7 @@ class StoryFirstTimeNavigationViewTest {

testSubject.setBlurHash(BlurHash.parseOrNull("0000")!!)

assertFalse(testSubject.findViewById<View>(org.thoughtcrime.securesms.R.id.edu_blur_hash).visible)
assertFalse(testSubject.findViewById<View>(R.id.edu_blur_hash).visible)
}

@Test
Expand All @@ -94,8 +80,8 @@ class StoryFirstTimeNavigationViewTest {

testSubject.setBlurHash(BlurHash.parseOrNull("0000")!!)

val blurHashView = testSubject.findViewById<ImageView>(org.thoughtcrime.securesms.R.id.edu_blur_hash)
verify(requestBuilder).into(eq(blurHashView))
val blurHashView = testSubject.findViewById<ImageView>(R.id.edu_blur_hash)
verify { requestBuilder.into(blurHashView) }
}

@Test
Expand All @@ -105,9 +91,9 @@ class StoryFirstTimeNavigationViewTest {

testSubject.setBlurHash(null)

val blurHashView = testSubject.findViewById<ImageView>(org.thoughtcrime.securesms.R.id.edu_blur_hash)
val blurHashView = testSubject.findViewById<ImageView>(R.id.edu_blur_hash)
assertFalse(blurHashView.visible)
verify(requestManager).clear(blurHashView)
verify { requestManager.clear(blurHashView) }
}

@Test
Expand All @@ -122,8 +108,8 @@ class StoryFirstTimeNavigationViewTest {

testSubject.setBlurHash(BlurHash.parseOrNull("0000")!!)

val blurHashView = testSubject.findViewById<ImageView>(org.thoughtcrime.securesms.R.id.edu_blur_hash)
verify(requestBuilder, never()).into(eq(blurHashView))
val blurHashView = testSubject.findViewById<ImageView>(R.id.edu_blur_hash)
verify(exactly = 0) { requestBuilder.into(blurHashView) }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import android.content.Intent
import android.net.Uri
import androidx.fragment.app.Fragment
import androidx.test.core.app.ApplicationProvider
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.signal.core.util.Base64
Expand All @@ -32,11 +32,10 @@ import java.util.Optional
@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class StoryContextMenuTest {

private val context: Context = ApplicationProvider.getApplicationContext()
private val intentCaptor = argumentCaptor<Intent>()
private val fragment: Fragment = mock {
on { requireContext() } doReturn context
private val intentSlot = slot<Intent>()
private val fragment = mockk<Fragment>(relaxUnitFun = true) {
every { requireContext() } returns this@StoryContextMenuTest.context
}

@Test
Expand All @@ -60,9 +59,9 @@ class StoryContextMenuTest {
StoryContextMenu.share(fragment, storyRecord)

// THEN
verify(fragment).startActivity(intentCaptor.capture())
val chooserIntent: Intent = intentCaptor.firstValue
val targetIntent: Intent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
verify { fragment.startActivity(capture(intentSlot)) }
val chooserIntent = intentSlot.captured
val targetIntent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
assertEquals(PartAuthority.getAttachmentPublicUri(PartAuthority.getAttachmentDataUri(attachmentId)), targetIntent.getParcelableExtraCompat(Intent.EXTRA_STREAM, Uri::class.java))
assertEquals(MediaUtil.IMAGE_JPEG, targetIntent.type)
assertTrue(Intent.FLAG_GRANT_READ_URI_PERMISSION and chooserIntent.flags == Intent.FLAG_GRANT_READ_URI_PERMISSION)
Expand All @@ -81,9 +80,9 @@ class StoryContextMenuTest {
StoryContextMenu.share(fragment, storyRecord)

// THEN
verify(fragment).startActivity(intentCaptor.capture())
val chooserIntent: Intent = intentCaptor.firstValue
val targetIntent: Intent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
verify { fragment.startActivity(capture(intentSlot)) }
val chooserIntent = intentSlot.captured
val targetIntent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
assertEquals(expected, targetIntent.getStringExtra(Intent.EXTRA_TEXT))
}

Expand All @@ -101,8 +100,8 @@ class StoryContextMenuTest {
StoryContextMenu.share(fragment, storyRecord)

// THEN
verify(fragment).startActivity(intentCaptor.capture())
val chooserIntent: Intent = intentCaptor.firstValue
verify { fragment.startActivity(capture(intentSlot)) }
val chooserIntent: Intent = intentSlot.captured
val targetIntent: Intent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
assertEquals(expected, targetIntent.getStringExtra(Intent.EXTRA_TEXT))
}
Expand All @@ -123,8 +122,8 @@ class StoryContextMenuTest {
StoryContextMenu.share(fragment, storyRecord)

// THEN
verify(fragment).startActivity(intentCaptor.capture())
val chooserIntent: Intent = intentCaptor.firstValue
verify { fragment.startActivity(capture(intentSlot)) }
val chooserIntent: Intent = intentSlot.captured
val targetIntent: Intent = chooserIntent.getParcelableExtraCompat(Intent.EXTRA_INTENT, Intent::class.java)!!
assertEquals(expected, targetIntent.getStringExtra(Intent.EXTRA_TEXT))
}
Expand Down
Loading

0 comments on commit 25e1982

Please sign in to comment.