question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't work for Android tests

See original GitHub issue

I am using this lib successfully for unit tests, but when I try to use it for an instrumented Android test, I get the following: Use:

import com.nhaarman.mockito_kotlin.mock
val n: AppData = mock()

Error:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/nhaarman/mockito_kotlin/MockitoKt;
at com.myproject.tests.Test.testSomething(Test.kt:91)
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 org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
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:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at com.jakewharton.u2020.U2020TestRunner.onStart(U2020TestRunner.java:22)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.nhaarman.mockito_kotlin.MockitoKt" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.myproject.test-1/base.apk", zip file "/data/app/com.myproject-2/base.apk"],nativeLibraryDirectories=[/data/app/com.myproject.test-1/lib/x86, /data/app/com.myproject-2/lib/x86, /data/app/com.myproject.test-1/base.apk!/lib/x86, /data/app/com.myproject-2/base.apk!/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 34 more

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16

github_iconTop GitHub Comments

5reactions
fondesacommented, Nov 6, 2017

For the moment, you can do this workaround:

androidTestCompile("com.nhaarman:mockito-kotlin-kt1.1:1.5.0", {
    exclude group: 'org.mockito', module: 'mockito-core'
})
androidTestCompile "org.mockito:mockito-android:2.11.0"

I hope it will be supported soon, in case, I can submit a PR for it.

2reactions
bogdanzuraccommented, Jun 25, 2018

Am I missing something? This issue is still happening, even with the mockito-android dependency added:

    androidTestImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1", {
        exclude group: 'org.mockito', module: 'mockito-core'
    })
    androidTestImplementation 'org.mockito:mockito-android:2.19.0'

Error log:

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         : 4.4.116-g6999c7c9f794
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't run unit tests in Android Studio - Stack Overflow
You can work around the issue by first running Make project and then Run each time you want to run the unit tests....
Read more >
Test in Android Studio
Android Studio is designed to make testing simple. It contains many features to simplify how you create, run, and analyze tests.
Read more >
Instrumented tests don't work on API 30 using Gradle command
We have a set of instrumented tests that run fine on Android 7,8,9, and 10 but not on Android 11 / API 30...
Read more >
Unable to run common tests on Android via gutter icon in a ...
Create a new Mobile Android/iOS project via New Project Wizard in IDEA. Navigate to SampleTests.kt in commonTest . There are Run gutter icons...
Read more >
Test failures in Studio don't show test output when running ...
It is possible to workaround the issue by unchecking "Run Android Instrumented tests using gradle" in settings. Issue summary. Links (3).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found