Hey guys, I am getting weird issue when running on...
# code-coverage
v
Hey guys, I am getting weird issue when running on my unit test. I have this function
Copy code
internal fun getPendingStatusAction(
status: XYZ
): (() -> Unit)? {
    var action: (() -> Unit)? = null
    this.yo = yo
    if (isAwaitingIdVerification(status)) {
        action = {
            <http://router.xyz|router.xyz>()
        }
    } else if (status == XYZ.PURCHASE) {
        action = {
            <http://router.xyz|router.xyz>()
        }
    }
    return action
}
When I run single unit test it complete fine, but when I run whole file unit test it failed.
Copy code
@Test
fun `getPendingStatusAction - `() {
    // STUBBING
    val mockStatus = XYZ.VERIFICATION
    every { subject.isAwaitingIdVerification(any()) } returns true
    // EXECUTION
    val action = subject.getPendingStatusAction(mockStatus)
    action?.invoke()

    // VERIFICATION
    verify {
        <http://router.xyz|router.xyz>()
            )
    }
}
Verification failed: call 1 of 2: Router(mockRouter#172).xyz) was not called
java.lang.AssertionError: Verification failed: call 1 of 2: Router(mockRouter#172).xyz) was not called
d
We are at a disadvantage to help you diagnose a problem with the whole file, unless you share the whole file (as a text snippet, please). Usually in such scenarios there has been some stateful interaction between two or more tests. Unit Tests should always be as independent as possible.
I assume you have more than one test defined in the file; do they share any instance variables?
k
The error message you get says "call 1 of 2" but you have only 1 call in your
verify
block. So you are probably looking at the wrong part of your code. Check the stack trace.
v
Copy code
import android.app.Activity
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.abc.app.analytics.Analytics
import com.abc.app.navigation.Router
import io.mockk.*
import io.mockk.impl.annotations.MockK
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.koin.core.context.loadKoinModules
import org.koin.dsl.module
import org.koin.test.KoinTestRule
import kotlin.test.assertEquals

class PlanHelperTest {

    companion object {
        private const val TITLE = "title"
        private const val MARKET_ID = "market_id"
        private const val SUBSCRIPTION_ID = "subscription_id"
    }

    @get:Rule
    val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()

    @get:Rule
    val koinTestRule = KoinTestRule.create()

    private lateinit var subject: PlanHelper
    @MockK
    private lateinit var mockRouter: Router
    @MockK
    private lateinit var mockActivity: Activity

    @Before
    fun setUp() {
        MockKAnnotations.init(this, relaxed = true)
      mockkObject(PlanHelper)
        loadKoinModules(module {
            listOf(
                factory { mockRouter },
                factory { mockAnalytics }
            )
        })
        subject = PlanHelper
    }

    @Test
    fun `getPendingStatusAction - WHEN is awaiting id verification, THEN returns action accordingly`() {
        // STUBBING
        val mockStatus = XYZ.VERIFICATION
        every { subject.isAwaitingIdVerification(any()) } returns true
        // EXECUTION
        val action = subject.getPendingStatusAction(mockStatus)
        action?.invoke()

        // VERIFICATION
        verify {
            <http://mockRouter.xyz|mockRouter.xyz>()
        }
    }

    @Test
    fun `getPendingStatusAction - WHEN is not supported status`() {
        // STUBBING
        val mockStatus = XYZ.VERIFICATION
        every { subject.isAwaitingIdVerification(any()) } returns false
        // EXECUTION
        val action = subject.getPendingStatusAction(mockStatus)
        // VERIFICATION
        assertEquals(TITLE, subject.treatmentPlanName)
        assertEquals(null, action)
    }
}
@darkmoon_uk I added my code when I run both code my first unit test fail and if run single then it pass the test case.
@Klitos Kyriacou I tried but nothing works to me. I am sharing my stacktrace file
k
The failed test is called
com.abc.app.PlanHelperTest.getPendingStatusAction - WHEN is awaiting id verification, THEN returns action accordingly
. This is not one of the tests you've showed us.
v
Oh sorry I fixed the my unit test case description. It's same, I just added the description. Sorry about that.
@Klitos Kyriacou did you get it ?
k
This could be something related to Koin (perhaps the
router
used by
subject
is not the
mockRouter
) but as I don't know Koin, I can't help any further. Sorry about that.
v
Sure no problem @Klitos Kyriacou ✌️. thanks a million
533 Views