6

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

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

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 toString in android.text.SpannableString not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.text.SpannableString.toString(SpannableString.java)
at java.lang.String.valueOf(String.java:2994)
at view_model.ViewModelTest$When getCredits() returns multiple invites$1.invokeSuspend(ViewModelTest.kt:113)
at ViewModelTest$When getCredits() returns multiple invites$1.invoke(ViewModelTest.kt)
at kotlinx.coroutines.test.TestBuildersKt$runBlockingTest$deferred$1.invokeSuspend(TestBuilders.kt:50)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.test.TestCoroutineDispatcher.dispatch(TestCoroutineDispatcher.kt:50)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:305) ...

The error occurs on showInvitesTextValue variable initialization.

How can I use SpannableString class on Unit Tests?

  • Does this answer your question? [Prevent stubbing of equals method](https://stackoverflow.com/questions/19778592/prevent-stubbing-of-equals-method) – emandt Apr 12 '21 at 15:15
  • @emandt, unfortunately, your answer can't help to solve my issue, beacuse my problem are on SpannableString import, and not on equals comparation. – André Felipe Apr 12 '21 at 15:22
  • You said that "your problem is when you trying to compare 2 Spannables" and the StackStrace is very clear where the Exception is raised: at "SpannableString.equals()". If you never call "Spannable.equals()" it doesn't mean a library or a component doesn't call it ;) – emandt Apr 12 '21 at 15:26
  • @emandt, I'm sorry, because I copy the stacktrace from another test. I edit my question with the write stacktrace. – André Felipe Apr 12 '21 at 16:14
  • Why do not open the suggested link from Stacktrace? http://g.co/androidstudio/not-mocked You will read that "android.jar" is empty so all Methods should be mocked by you or they will throw Exceptions. All is well explained in that link.... (I personally never used Unit Test, Mocking or other similar things) – emandt Apr 12 '21 at 21:52

0 Answers0