Skip to content

Commit

Permalink
test(mockk): migrate mockito tests
Browse files Browse the repository at this point in the history
SDK-149

Co-authored-by: Andras Sarro <[email protected]>
  • Loading branch information
davidSchuppa and LordAndras committed Jan 14, 2025
1 parent 1e941fe commit 1cf71e5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ import com.emarsys.mobileengage.iam.dialog.IamDialogProvider
import com.emarsys.mobileengage.iam.webview.MessageLoadedListener
import com.emarsys.testUtil.AnnotationSpec
import com.emarsys.testUtil.fake.FakeActivity
import com.emarsys.testUtil.mockito.anyNotNull
import com.emarsys.testUtil.mockito.whenever
import io.kotest.matchers.shouldBe
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.util.concurrent.CountDownLatch

class OverlayInAppPresenterTest : AnnotationSpec() {
Expand All @@ -41,15 +37,22 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
@Before
fun setUp() {
concurrentHandlerHolder = ConcurrentHandlerHolderFactory.create()
mockIamDialog = mock()
mockIamDialogProvider = mock {
on { provideDialog(any(), any(), any(), any()) } doReturn mockIamDialog
}
mockTimestampProvider = mock()
mockCurrentActivityProvider = mock()
mockIamDialog = mockk(relaxed = true)
mockIamDialogProvider = mockk(relaxed = true)
every {
mockIamDialogProvider.provideDialog(
any(),
any(),
any(),
any()
)
} returns mockIamDialog

mockTimestampProvider = mockk(relaxed = true)
mockCurrentActivityProvider = mockk(relaxed = true)

whenever(mockIamDialog.loadInApp(any(), any(), any(), any())).thenAnswer {
(it.getArgument(2) as MessageLoadedListener).onMessageLoaded()
every { mockIamDialog.loadInApp(any(), any(), any(), any()) } answers {
(args[2] as MessageLoadedListener).onMessageLoaded()
}

inAppPresenter = OverlayInAppPresenter(
Expand All @@ -67,7 +70,7 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
var callbackCalled = false
Thread {
scenario.onActivity { activity ->
whenever(mockCurrentActivityProvider.activity()).thenReturn(activity)
every { mockCurrentActivityProvider.activity() } returns activity

inAppPresenter.present(
"1",
Expand Down Expand Up @@ -95,7 +98,7 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
var callbackCalled = false
Thread {
scenario.onActivity { activity ->
whenever(mockCurrentActivityProvider.activity()).thenReturn(activity)
every { mockCurrentActivityProvider.activity() } returns activity

inAppPresenter.present(
"1",
Expand All @@ -112,17 +115,17 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
}.start()
countDownLatch.await()
scenario.close()
verify(mockIamDialog).showNow(any<FragmentManager>(), any())
verify { mockIamDialog.showNow(any<FragmentManager>(), any()) }
callbackCalled shouldBe true
}

@Test
fun testPresent_shouldNotShowDialog_whenActivity_isUsed() {
val activity: Activity = mock()
val activity: Activity = mockk(relaxed = true)
val countDownLatch = CountDownLatch(1)
var callbackCalled = false
Thread {
whenever(mockCurrentActivityProvider.activity()).thenReturn(activity)
every { mockCurrentActivityProvider.activity() } returns activity

inAppPresenter.present(
"1",
Expand All @@ -138,34 +141,31 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
}.start()
countDownLatch.await()

verify(mockIamDialog, times(0)).showNow(any<FragmentManager>(), any())
verify(exactly = 0) { mockIamDialog.showNow(any<FragmentManager>(), any()) }
callbackCalled shouldBe true

}

@Test
fun testPresent_shouldNotShowDialog_whenAnotherDialog_isAlreadyShown() {
val activity: AppCompatActivity = mock()
val fragmentManager: FragmentManager = mock()
val fragment: Fragment = mock()
val activity: AppCompatActivity = mockk(relaxed = true)
val fragmentManager: FragmentManager = mockk(relaxed = true)
val fragment: Fragment = mockk(relaxed = true)
val countDownLatch = CountDownLatch(1)
var callbackCalled = false
Thread {
whenever(
every {
mockIamDialogProvider.provideDialog(
anyNotNull(),
any<String>(),
any(),
any(),
any()
)
).thenReturn(
mockIamDialog
)
whenever(mockCurrentActivityProvider.activity()).thenReturn(activity)
whenever(activity.supportFragmentManager).thenReturn(fragmentManager)
whenever(fragmentManager.findFragmentByTag("MOBILE_ENGAGE_IAM_DIALOG_TAG")).thenReturn(
fragment
)
} returns mockIamDialog

every { mockCurrentActivityProvider.activity() } returns activity
every { activity.supportFragmentManager } returns fragmentManager
every { fragmentManager.findFragmentByTag("MOBILE_ENGAGE_IAM_DIALOG_TAG") } returns fragment

inAppPresenter.present(
"1",
Expand All @@ -181,34 +181,31 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
}.start()
countDownLatch.await()

verify(mockIamDialog, times(0)).showNow(any<FragmentManager>(), any())
verify(exactly = 0) { mockIamDialog.showNow(any<FragmentManager>(), any()) }
callbackCalled shouldBe true

}

@Test
fun testPresent_shouldNotShowDialog_whenFragmentManager_isInSavedState() {
val activity: AppCompatActivity = mock()
val fragmentManager: FragmentManager = mock()
val activity: AppCompatActivity = mockk(relaxed = true)
val fragmentManager: FragmentManager = mockk(relaxed = true)
val countDownLatch = CountDownLatch(1)
var callbackCalled = false
Thread {
whenever(
every {
mockIamDialogProvider.provideDialog(
anyNotNull(),
any<String>(),
any(),
any(),
any()
)
).thenReturn(
mockIamDialog
)
whenever(mockCurrentActivityProvider.activity()).thenReturn(activity)
whenever(activity.supportFragmentManager).thenReturn(fragmentManager)
whenever(fragmentManager.isStateSaved).thenReturn(true)
whenever(fragmentManager.findFragmentByTag("MOBILE_ENGAGE_IAM_DIALOG_TAG")).thenReturn(
null
)
} returns mockIamDialog

every { mockCurrentActivityProvider.activity() } returns activity
every { activity.supportFragmentManager } returns fragmentManager
every { fragmentManager.isStateSaved } returns true
every { fragmentManager.findFragmentByTag("MOBILE_ENGAGE_IAM_DIALOG_TAG") } returns null

inAppPresenter.present(
"1",
Expand All @@ -224,8 +221,7 @@ class OverlayInAppPresenterTest : AnnotationSpec() {
}.start()
countDownLatch.await()

verify(mockIamDialog, times(0)).showNow(any<FragmentManager>(), any())
verify(exactly = 0) { mockIamDialog.showNow(any<FragmentManager>(), any()) }
callbackCalled shouldBe true

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.emarsys.mobileengage.iam
import com.emarsys.core.provider.timestamp.TimestampProvider

import com.emarsys.testUtil.AnnotationSpec
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify

class PushToInAppActionTest : AnnotationSpec() {

Expand All @@ -27,8 +26,8 @@ class PushToInAppActionTest : AnnotationSpec() {

@Before
fun setup() {
mockOverlayInAppPresenter = mock()
mockTimestampProvider = mock()
mockOverlayInAppPresenter = mockk(relaxed = true)
mockTimestampProvider = mockk(relaxed = true)

pushToInAppAction = PushToInAppAction(
mockOverlayInAppPresenter, CAMPAIGN_ID, HTML, SID, URL, mockTimestampProvider,
Expand All @@ -38,10 +37,20 @@ class PushToInAppActionTest : AnnotationSpec() {

@Test
fun testExecute_runsWithCorrectParams() {
whenever(mockTimestampProvider.provideTimestamp()).doReturn(TIMESTAMP)
pushToInAppAction.execute(mock())

verify(mockOverlayInAppPresenter).present(CAMPAIGN_ID, SID, URL,null, TIMESTAMP, HTML,null)
every { mockTimestampProvider.provideTimestamp() } returns TIMESTAMP
pushToInAppAction.execute(mockk(relaxed = true))

verify {
mockOverlayInAppPresenter.present(
CAMPAIGN_ID,
SID,
URL,
null,
TIMESTAMP,
HTML,
null
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class IamJsBridgeTest : AnnotationSpec() {
private lateinit var mockOnOpenExternalUrlListener: JSCommand
private lateinit var mockCopyToClipboardListener: JSCommand
private lateinit var mockOnMEEventListener: JSCommand
private val slot = slot<JSONObject>()

@Before
fun setUp() {
Expand Down Expand Up @@ -169,6 +168,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testTriggerAppEvent_shouldInvokeCallback_onSuccess() {
val slot = slot<JSONObject>()
val id = "123456789"
val json = JSONObject().put("id", id).put("name", "value")

Expand All @@ -184,6 +184,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testTriggerMeEvent_shouldInvokeCallback_onSuccess() {
val slot = slot<JSONObject>()
val id = "123456789"
val json = JSONObject().put("id", id).put("name", "value")

Expand All @@ -199,6 +200,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testButtonClicked_shouldInvokeCallback_onSuccess() {
val slot = slot<JSONObject>()
val id = "12346789"
val buttonId = "987654321"
val json = JSONObject().put("id", id).put("buttonId", buttonId)
Expand All @@ -214,6 +216,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testOpenExternalLink_shouldInvokeCallback_onSuccess() {
val slot = slot<JSONObject>()
val id = "12346789"
val url = "https://emarsys.com"
val json = JSONObject().put("id", id).put("url", url).put("keepInAppOpen", false)
Expand All @@ -229,6 +232,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testTriggerAppEvent_shouldInvokeCallback_whenNameIsMissing() {
val slot = slot<JSONObject>()
val id = "123456789"
val json = JSONObject().put("id", id)
jsBridge.triggerAppEvent(json.toString())
Expand All @@ -244,6 +248,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testTriggerMeEvent_shouldInvokeCallback_whenNameIsMissing() {
val slot = slot<JSONObject>()
val id = "123456789"
val json = JSONObject().put("id", id)

Expand All @@ -260,6 +265,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testButtonClicked_shouldInvokeCallback_whenButtonIdIsMissing() {
val slot = slot<JSONObject>()
val id = "12346789"
val json = JSONObject().put("id", id)
jsBridge.buttonClicked(json.toString())
Expand All @@ -275,6 +281,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testOpenExternalLink_shouldInvokeCallback_whenUrlIsMissing() {
val slot = slot<JSONObject>()
val id = "12346789"
val json = JSONObject().put("id", id).put("keepInAppOpen", false)
jsBridge.openExternalLink(json.toString())
Expand All @@ -290,6 +297,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testCopyToClipboard_shouldInvokeCallback_onSuccess() {
val slot = slot<JSONObject>()
val id = "12346789"
val json = JSONObject().put("id", id).put("text", "testText")
jsBridge.copyToClipboard(json.toString())
Expand All @@ -304,6 +312,7 @@ class IamJsBridgeTest : AnnotationSpec() {

@Test
fun testCopyToClipboard_shouldInvokeCallback_onError_whenTextIsMissing() {
val slot = slot<JSONObject>()
val id = "12346789"
val json = JSONObject().put("id", id)
jsBridge.copyToClipboard(json.toString())
Expand Down
Loading

0 comments on commit 1cf71e5

Please sign in to comment.