Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito 2 for Android Instrumentation test : Could not initialize plugin: interface org.mockito.plugins.MockMaker

I'm using Mockito 2 and Espresso for Unit tests, and met a problem trying to use Mockito 2 in Instrumentation test. While running the test, I'm getting this error

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

And here is the test code.

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

I've googled for more information and found out, that I need to use mockito-android instead of mockito core, and added those lines in application's build.gradle file

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

where libMockitoAndroidVersion = 2.16.0

Also, I've added org.mockito.plugins.MockMaker file into the androidTest resource directory to work with Kotlin's classes.

enter image description here

Is there any way to fix this issue? Any help will be appreciated.

like image 720
Arshak92 Avatar asked Apr 12 '18 13:04

Arshak92


2 Answers

Working with Android replace

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

for

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

My version was: $mockito_core_version = '2.28.2'

like image 59
Victor R. Oliveira Avatar answered Sep 18 '22 05:09

Victor R. Oliveira


I've got similar problem. I my case that was invalid declaration in resource/mockito-extensions/org.mockito.plugins.MockMaker file. During some refactoring process IDE replaced correct value:

mock-maker-inline

with this one:

infrastructure-maker-inline 

I've noticed that accidental change while reviewing the commit diff because it didn't look suspicious for first look.

like image 41
Greg Barski Avatar answered Sep 22 '22 05:09

Greg Barski