代码之家  ›  专栏  ›  技术社区  ›  Arshak92

Mockito 2 for Android Instrumentation测试:无法初始化插件:interface org。莫基托。插件。MockMaker公司

  •  16
  • Arshak92  · 技术社区  · 7 年前

    我在单元测试中使用Mockito 2和Espresso,在仪器测试中使用Mockito 2时遇到了一个问题。运行测试时,我发现以下错误

    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 java.lang.reflect.Proxy.invoke(Proxy.java:913)
    at $Proxy5.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:232)
    at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:64)
    at org.mockito.Mockito.mock(Mockito.java:1864)
    at org.mockito.Mockito.mock(Mockito.java:1777)
    at com.armop.insight.views.activity.InsightActivityTest.testUserResultReturnsEmpty(InsightActivityTest.kt:66)
    at java.lang.reflect.Method.invoke(Native Method)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
    at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:369)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
    Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@5ea25e5
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:21)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:18)
    at org.mockito.internal.configuration.plugins.Plugins.getMockMaker(Plugins.java:34)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    ... 38 more
    Caused by: org.mockito.exceptions.base.MockitoInitializationException:
    Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
    
    
    IMPORTANT INFORMATION FOR ANDROID USERS:
    
    The regular Byte Buddy mock makers cannot generate code on an Android VM!
    To resolve this, please use the 'mockito-android' dependency for your application:
    http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22mockito-android%22%20g%3A%22org.mockito%22
    
    Java               : 0.9
    JVM vendor name    : The Android Project
    JVM vendor version : 2.1.0
    JVM name           : Dalvik
    JVM version        : 0.9
    JVM info           : null
    OS name            : Linux
    OS version         : 3.18.91+
    
    at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:171)
    at java.lang.Class.newInstance(Native Method)
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49)
    ... 44 more
    Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
    at net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm$ForLegacyVm.resolve(ByteBuddyAgent.java:940)
    at net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm.resolve(ByteBuddyAgent.java:925)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:352)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:320)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:306)
    at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:101)
    ... 46 more
    Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.armop.debug.test-GWx-9OMGi9FTj01c1acroQ==/base.apk", zip file "/data/app/com.armop.debug-28ywc-fJ-zzWAgMPBESlGQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.armop.debug.test-GWx-9OMGi9FTj01c1acroQ==/lib/x86, /data/app/com.armop.debug-28ywc-fJ-zzWAgMPBESlGQ==/lib/x86, /data/app/com.armop.debug.test-GWx-9OMGi9FTj01c1acroQ==/base.apk!/lib/x86, /data/app/com.armop.debug-28ywc-fJ-zzWAgMPBESlGQ==/base.apk!/lib/x86, /system/lib, /vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 52 more
    

    这是测试代码。

     @Test
    fun testUserResultReturnsEmpty() {
    
        val userMock = Mockito.mock(UserModel::class.java)
        Mockito.`when`(ArmOpApplication.getUserFromPrefs()).thenReturn(userMock)
    
        mActivityRule.launchActivity(Intent())
        mCountDownLatch.await(3000, TimeUnit.MILLISECONDS)
    
        assertNotNull(mActivityRule.activity.mInsightCategories)
    
        assertNull(mActivityRule.activity.mData)
        assertNull(mActivityRule.activity.insightsRecyclerView.adapter)
    }
    

    我在谷歌上搜索了更多信息,发现我需要使用mockito android而不是mockito core,并在应用程序的构建中添加了这些行。gradle文件

    androidTestImplementation "org.mockito:mockito-android:${libMockitoAndroidVersion}"
    androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
    
    where libMockitoAndroidVersion = 2.16.0
    

    此外,我还添加了org。莫基托。插件。MockMaker文件放入androidTest资源目录中,以使用Kotlin的类。

    enter image description here

    有没有办法解决这个问题?任何帮助都将不胜感激。

    6 回复  |  直到 6 年前
        1
  •  7
  •   Victor Oliveira    5 年前

    使用Android替换

    androidTestImplementation "org.mockito:mockito-core:$mockito_core_version"
    androidTestImplementation "org.mockito:mockito-inline:$mockito_core_version"
    

    对于

    androidTestImplementation "org.mockito:mockito-android:$mockito_core_version"
    

    我的版本是: $mockito_core_version = '2.28.2'

        2
  •  3
  •   Benny    3 年前

    我遇到了这个问题,这里的任何解决方案都不适合我。我的问题是我混合了两个mockito库

    //BAD CODE
    testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
    testImplementation "org.mockito:mockito-android:3.3.3"
    
    
    //GOOD CODE - just use one
    testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
    
        3
  •  2
  •   Greg Barski    6 年前

    我也有类似的问题。我的案例在 resource/mockito-extensions/org.mockito.plugins.MockMaker 文件在某些重构过程中,IDE替换了正确的值:

    mock-maker-inline
    

    使用此选项:

    infrastructure-maker-inline 
    

    我在查看提交差异时注意到了意外的更改,因为它在第一次查看时看起来并不可疑。

        4
  •  2
  •   Nokuap    5 年前

    这对我很有帮助

    androidTestImplementation("org.koin:koin-test:1.0.2")
    { exclude("org.mockito", "mockito-inline") }
    androidTestImplementation("org.mockito:mockito-android:2.24.5")
    

    Koin表示一些包含org的库。莫基托

        5
  •  1
  •   kaulctin    4 年前

    在stacktrace中,我发现:

    Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration
    

    受此启发( #824 Exception when using mock-maker-inline ),我通过添加工具解决了相同的问题。jar到intellij中的我的类路径

    add tools.jar to classpath

        6
  •  0
  •   Rafael Winterhalter    7 年前

    从堆栈跟踪来看,您似乎使用的是内联字节好友模拟生成器,它在Android上不起作用。请确保您没有以某种方式传递它,因为两个可用的插件都会首先加载添加到类路径的插件。