无法初始化MockMaker

问题描述 投票:0回答:4

这是我的测试课:

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()
    }
}

具有以下依赖项:

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'

我创建了

src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker
,里面有
mock-maker-inline

测试类失败是因为

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.)

如何解决这个问题?所有这些答案都没有帮助。

android integration-testing android-testing mockito-kotlin
4个回答
20
投票

这有效:

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


0
投票

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

testImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'

0
投票

我最近遇到了同样的问题,在 Mockito 核心旁边显式添加了以下依赖项并且它起作用了

    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy</artifactId>
        <version>1.12.18</version>
    </dependency>
    <dependency>
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>3.3</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy-agent</artifactId>
        <version>1.12.18</version>
        <scope>test</scope>
    </dependency>

0
投票

添加 org.mockito:mockito-inline:4.0.0 作为测试依赖项为我解决了这个问题。

// Mockito
androidTestImplementation("org.mockito:mockito-android:5.10.0")
testImplementation("org.mockito:mockito-android:5.10.0")
testImplementation("org.mockito:mockito-inline:4.0.0")
© www.soinside.com 2019 - 2024. All rights reserved.