Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot initialize MockMaker

This is my testing class:

class RocketListVMTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var sut: RocketListVM
    private var activeOnlyToggle = false

    private val repo: Repo = mock()

    @Before
    fun setUp() {
        sut = RocketListVM(repo)
        activeOnlyToggle = false
    }

    @Test
    fun toggleActiveOnlyWithTrueCallsRepository() {
        sut.toggleActiveOnly(true)

        verify(repo).getActiveOnlyLocalRockets()
    }
}

With the following dependencies:

androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'

I have created src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker with mock-maker-inline inside.

The test class fails because

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@dd6cba3
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 
Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

How to fix this problem? None of the SO answers helped.

like image 345
Le Nguyen Duy Anh Avatar asked Feb 24 '21 01:02

Le Nguyen Duy Anh


People also ask

What is MockMaker?

MockMaker is an extension point that makes it possible to use custom dynamic proxies and avoid using the default byte-buddy/asm/objenesis implementation. For example, the android users can use a MockMaker that can work with Dalvik virtual machine and hence bring Mockito to android apps developers.

How do you use a mock maker inline?

Option 2: Adding a file to enable the Mock maker inlineCreate a resources directory in your test directory if you do not have one. In resources create the directory mockito-extensions and in that directory the file org. mockito. plugins.


1 Answers

This works:

dependencies {
    testImplementation 'org.mockito:mockito-core:3.8.0'
    androidTestImplementation 'org.mockito:mockito-android:3.8.0'
}

There are two packages: mockito-core is for test, while mockito-android is for android test.

ref java.lang.IllegalStateException: Could not initialize plugin: MockMaker

like image 91
Yin Shen Avatar answered Sep 18 '22 11:09

Yin Shen