Messy Experience with mockito

everything worked fine with testing until i got to mock(...)

then everything became messy really…

even now after the app is working fine, i still get errors in testing… (if it tests)

usually i get

Unresolved reference: mockito

what is wrong here?

 package com.bignerdranch.android.beatbox

import org.hamcrest.CoreMatchers.`is`
import org.junit.Assert.*

import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify


/*
testing a unit page: 397
 */
class SoundViewModelTest {

    private lateinit var sound: Sound
    private lateinit var subject: SoundViewModel
    private lateinit var beatBox: BeatBox

    @Before
    fun setUp() {

        beatBox= mock(BeatBox::class.java)
        sound = Sound("assetPath")
        subject = SoundViewModel(beatBox)
        subject.sound = sound
    }

    @Test
    fun exposesSoundNameAsTitle(){
        //assertThat(subject.title, `is`(sound.name))
        assertSame(subject.title, sound.name)
    }

    @Test
    fun callsBeatBoxPlayButtonClicked(){
        subject.onButtonClicked()

        verify(beatBox).play(sound)
    }
}

note: to remove the unresolved reference for mockito, i had to add 2 dependency in gradle

androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1' 

but it gave me all the tests error

January 2022 as I write this, so the 4th edition is going on 3 years old at this point. I also ran into an issue with mock() by the time I got to the sections between Listing 20.12 and Figure 20.5. I was seeing this type of error in the console after running the test:

Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
at com.sun.proxy.$Proxy15.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:240)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:228)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
at org.mockito.Mockito.mock(Mockito.java:1907)
at org.mockito.Mockito.mock(Mockito.java:1816)
at com.bignerdranch.android.beatbox.SoundViewModelTest.setUp(SoundViewModelTest.kt …

The fix for me was to update my app’s build.gradle and go from these two lines from the book:

testImplementation “org.mockito:mockito-core:2.25.0”
testImplementation “org.mockito:mockito-inline:2.25.0”

To the current stable release of mockito as of this writing:

testImplementation “org.mockito:mockito-core:4.2.0”
testImplementation “org.mockito:mockito-inline:4.2.0”

I was able to get the tests functioning as they show in Figure 20.6.

(Don’t forget to synch your gradle file after you make the above two changes).

1 Like