lundi 12 avril 2021

SpannableString doesn't work on Android Unit Test

I try to compare two SpannableStrings on my unit test and I receive the error:

java.lang.RuntimeException: Method equals in android.text.SpannableString not mocked.

After debug, I verify that I can't use the SpannableString class at all on Unit Tests.

Test Implementation:

    @Test
    fun `When getCredits() returns multiple invites`() = testDispatcher.testCoroutineDispatcher.runBlockingTest {
        //Change mock response
        repository.setMultipleInvitesResponse()

        //Call ViewModel method to return credits
        testDispatcher.testCoroutineDispatcher.pauseDispatcher()
        viewModel.getCredits()
        testDispatcher.testCoroutineDispatcher.resumeDispatcher()

        //Get remote SpannableString invites
        val invites = "You have 5 invites"

        // Initialize viewModel.showInvitesText.value
        val showInvitesTextValue: SpannableString = viewModel.showInvitesText.value

        //Compare view model showInvitesText.value with invites
        assertEquals(showInvitesTextValue.toString(), invites)
    }

Stacktrace:

java.lang.RuntimeException: Method equals in android.text.SpannableString not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.text.SpannableString.equals(SpannableString.java)
at org.junit.Assert.isEquals(Assert.java:131)
at org.junit.Assert.equalsRegardingNull(Assert.java:127)
at org.junit.Assert.assertEquals(Assert.java:111)
at org.junit.Assert.assertEquals(Assert.java:144)
at view_model.ViewModelTest$When getCredits() returns multiple invites$1.invokeSuspend(ViewModelTest.kt:110)
at view_model.ViewModelTest$When getCredits() returns multiple invites$1.invoke(ViewModelTest.kt)
at kotlinx.coroutines.test.TestBuildersKt$runBlockingTest$deferred$1.invokeSuspend(TestBuilders.kt:50) ...

How can I use SpannableString class on Unit Tests?

Aucun commentaire:

Enregistrer un commentaire