问题
Android newbie here trying to use my favorite Java testing tools in Android. I am attempting to use Mockito 1.9.5 as outlined in the following blog post but cannot get the tests to run on my emulator (I currently do not have a physical device to test with either).
Mockit-Android Tutorial: http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/

I am able to execute all my normal Junit tests without issue however any of the tests I have leveraged Mockito for I receive the following:
Can't open dex cache '/data/dalvik-cache/data@data@com.trendium.peg@cache@Generated-621101.jar@classes.dex': No such file or directory
Unable to open or create cache for /data/data/com.trendium.peg/cache/Generated-621101.jar (/data/dalvik-cache/data@data@com.trendium.peg@cache@Generated-621101.jar@classes.dex)
failed: testStartable(com.trendium.peg.services.RatingServiceTest)
java.lang.AssertionError: java.lang.ClassNotFoundException: RemoteRestTask_Proxy in loader dalvik.system.DexClassLoader@40a4b610
I've done a great deal of Googling these exceptions, rebuilt my emulator, rebuilt projects, and a number of other various ideas but haven't had any luck in resolving this.
Side note: I am targeting SDK 11 and up, doubt this has impact but worth noting. My existing unit tests that do not leverage Mockito run without issue in same test run (28/28 ran, 7 failures).
Further analysis of the LogCat reveals a mockito cglib is evidently not on the classpath, however I am unsure of the next step at this moment:
03-25 09:10:42.990: W/dalvikvm(411): Unable to resolve superclass of Lorg/mockito/cglib/transform/AbstractProcessTask; (637)
03-25 09:10:43.000: W/dalvikvm(411): Link of class 'Lorg/mockito/cglib/transform/AbstractProcessTask;' failed
03-25 09:10:43.029: D/dalvikvm(411): GC_CONCURRENT freed 618K, 9% free 9226K/10055K, paused 4ms+6ms
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Cannot load class. Make sure it is in your apk. Class name: 'org.mockito.cglib.transform.AbstractProcessTask'. Message: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): java.lang.ClassNotFoundException: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.Class.classForName(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.Class.forName(Class.java:234)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:89)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:40)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:51)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:48)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.SimpleCache.get(SimpleCache.java:31)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:73)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:356)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3550)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.app.ActivityThread.access$2200(ActivityThread.java:123)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.os.Looper.loop(Looper.java:126)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.reflect.Method.invoke(Method.java:491)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at dalvik.system.NativeStart.main(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Caused by: java.lang.NoClassDefFoundError: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): ... 26 more
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Caused by: java.lang.ClassNotFoundException: org.mockito.cglib.transform.AbstractProcessTask in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.example.mine.test-1.apk:/data/app/com.example.mine-1.apk]
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): ... 26 more
Based on this error message I look at the Mockito source and note that it is using Ant's Task. However, I don't see Ant as a dependency of Mockito...
https://fisheye2.atlassian.com/browse/mockito/trunk/cglib-and-asm/src/org/mockito/cglib/transform/AbstractProcessTask.java?r=1430
Further debugging and I am blocked again with the following:
03-25 15:07:01.726: I/dalvikvm(703): Failed resolving Lorg/junit/internal/AssumptionViolatedException; interface 693 'Lorg/hamcrest/SelfDescribing;'
03-25 15:07:01.726: W/dalvikvm(703): Link of class 'Lorg/junit/internal/AssumptionViolatedException;' failed
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): Cannot load class. Make sure it is in your apk. Class name: 'org.junit.internal.AssumptionViolatedException'. Message: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): java.lang.ClassNotFoundException: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at java.lang.Class.classForName(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at java.lang.Class.forName(Class.java:234)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:89)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:40)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:51)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:48)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.SimpleCache.get(SimpleCache.java:31)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:73)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:356)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3550)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.app.ActivityThread.access$2200(ActivityThread.java:123)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.os.Looper.loop(Looper.java:126)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at java.lang.reflect.Method.invoke(Method.java:491)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): at dalvik.system.NativeStart.main(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): Caused by: java.lang.NoClassDefFoundError: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): ... 26 more
This would indicate that the hamcrst-core jar is not on the classpath, however I have properly added it the the test-project's libs directory:

https://github.com/junit-team/junit/blob/r4.11/src/main/java/org/junit/internal/AssumptionViolatedException.java
Please note that I am using the correct (from my understanding) jars for Junit-4.11 in regards to Mockito 1.9.5. https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency
回答1:
You have to make sure th Mockito's JAR is part of you APK dependencies.
If you are using Intellij, open project settings, and Module->Dependencies, and add Mockito as part of dependency.
If you are using obfuscation, you also have to make sure its not obfuscating mockito's class from your built apk.
回答2:
I had the same issues while I'm calling a native API without the mocking them -- make sure you have a mock of the native calls. Check also the linkage error and not only the Mockito exception.
回答3:
This issue may be caused by not having the libraries on your actual android device, even though you may have added them to Eclipse or Android Studio.
In my android test project I was able to fix this problem by creating a libs folder and putting the three required libraries in it. Be sure the name of your created folder is actually "libs" and NOT "lib"
Here's a link from the android developer site with more detailed directions for adding support libraries. http://developer.android.com/tools/support-library/setup.html
来源:https://stackoverflow.com/questions/15617610/mockito-on-android-emulator