web-dev-qa-db-fra.com

Mockito 2 pour Android Test d'instrumentation: impossible d'initialiser le plugin: interface org.mockito.plugins.MockMaker

J'utilise Mockito 2 et Espresso pour les tests unitaires et j'ai rencontré un problème en essayant d'utiliser Mockito 2 dans le test d'instrumentation. Pendant l'exécution du test, je reçois cette erreur

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

Et voici le code de test.

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

J'ai cherché sur Google pour plus d'informations et j'ai découvert que je devais utiliser mockito-Android au lieu de mockito core, et j'ai ajouté ces lignes dans le fichier build.gradle de l'application

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

where libMockitoAndroidVersion = 2.16.0

J'ai également ajouté le fichier org.mockito.plugins.MockMaker dans le répertoire de ressources androidTest pour travailler avec les classes de Kotlin.

enter image description here

Existe-t-il un moyen de résoudre ce problème? Toute aide serait appréciée.

9
Arshak92

À partir de la trace de la pile, il semble que vous utilisez le créateur de maquette en ligne Byte Buddy qui ne fonctionne pas sur Android. Assurez-vous que vous ne l'avez pas inclus de manière transitoire, car les deux plugins disponibles chargeraient d'abord celui qui est ajouté au chemin de classe.

0

Travailler avec 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"

Ma version était: $mockito_core_version = '2.28.2'

3
Victor R. Oliveira

Cela m'a aidé

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

Koin représente une bibliothèque contenant org.mockito

2
Nokuap

J'ai un problème similaire. I mon cas qui était une déclaration invalide en resource/mockito-extensions/org.mockito.plugins.MockMaker fichier. Au cours d'un processus de refactoring IDE a remplacé la valeur correcte:

mock-maker-inline

avec celui-ci:

infrastructure-maker-inline 

J'ai remarqué ce changement accidentel lors de l'examen de la différence de validation, car elle ne semblait pas suspecte à première vue.

2
Greg Barski