8

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.

4

2 回答 2

14

这有效:

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

有两个包:mockito-core 用于测试,而 mockito-android 用于 android 测试。

ref java.lang.IllegalStateException:无法初始化插件:MockMaker

于 2021-03-03T08:48:49.977 回答
0

我遇到了同样的问题,我已经通过在 build.gradle(应用程序级)中使用testImplementation而不是 androidTestImplementation 来解决它:

testImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
于 2021-12-02T04:33:36.670 回答