当前位置: 首页 > 知识库问答 >
问题:

用于Android测试的Mockito 2:无法初始化插件:interface org.Mockito.plugins.mockmaker

翟兴邦
2023-03-14

我正在使用Mockito2和Espresso进行单元测试,在尝试使用Mockito2进行检测测试时遇到了一个问题。在运行测试时,我得到了这个错误

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内核,并在应用程序的build.gradle文件中添加了这些行

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

where libMockitoAndroidVersion = 2.16.0

有什么方法可以解决这个问题吗?任何帮助都将不胜感激。

共有1个答案

鄂伟兆
2023-03-14

使用Android replace

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'

 类似资料:
  • 试图在AS上运行检测测试。 ExampleInstrumentedTest.java build.gradle 更新:注释行后- initMocks(this); 它的构建很好(也不例外),但上下文被嘲笑现在是空的。

  • 这是我的测试: 我使用的是NetBeans 8.2。我已经下载了mockito-core-2.7.0.jar,然后选择“test libraries”->“add jar”并添加了mockito-core-2.7.0.jar。 类路径和一切看起来都很好,我仍然得到异常。

  • 问题内容: 我在Cloudbees上设置了Jenkins Job,可以在那里成功签出并编译我的Android项目。现在,我想在android模拟器中运行一些JUnit测试并添加Android模拟器插件。我将“显示仿真器窗口”选项设置为false,但是在启动仿真器后,总是收到错误消息:“仿真器似乎没有启动;放弃”。有没有人在Cloudbees上使用Android构建经验? 构建日志: 问题答案: 将

  • 问题内容: 我是Android Studio的新手。设置完成后,当我尝试导入应用程序时,出现该错误,因此无法构建gradle。 错误:无法初始化类org.codehaus.groovy.runtime.InvokerHelper 我检查了Java的classpath设置是否正确。我正在运行Windows操作系统。有人知道错误的根源吗? 问题答案: 由于Jdk的版本较低,我遇到了这个问题。以前我安装

  • 我正在尝试执行以下操作: > 在mvn测试阶段将一些数据库脚本执行到hsqldb中 将该数据库用于测试目的 我能够配置maven,以便每次调用测试阶段时,所有脚本都成功执行,但是(当然有但是),我所有的测试都失败了。 我的配置: pom.xml 测试Spring配置: 输出: 但后来我所有的测试都失败了。我需要你的帮助,找出为什么它不起作用,并帮助我找到解决方案。非常感谢。 @安德鲁洛维诺夫 这是