Android 新手在这里尝试在 Android 中使用我最喜欢的 Java 测试工具。我正在尝试使用以下博客文章中概述的 Mockito 1.9.5,但无法在我的模拟器上运行测试(我目前没有物理设备可以测试)。
Mockit-Android 教程: http: //www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/
我能够毫无问题地执行所有正常的 Junit 测试,但是我利用 Mockito 进行的任何测试都收到以下信息:
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
我已经对这些异常进行了大量的谷歌搜索,重建了我的模拟器,重建了项目,以及许多其他各种想法,但没有任何运气来解决这个问题。
旁注:我的目标是 SDK 11 及更高版本,怀疑这会产生影响,但值得注意。我现有的不利用 Mockito 的单元测试在相同的测试运行中运行没有问题(28/28 运行,7 次失败)。
对 LogCat 的进一步分析表明,mockito cglib 显然不在类路径中,但是我现在不确定下一步:
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
基于此错误消息,我查看了 Mockito 源并注意到它正在使用 Ant 的任务。但是,我不认为 Ant 是 Mockito 的依赖项......
进一步调试,我再次被以下内容阻止:
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
这表明 hamcrst-core jar 不在类路径中,但是我已将其正确添加到测试项目的 libs 目录中:
请注意,关于 Mockito 1.9.5,我正在为 Junit-4.11 使用正确的(根据我的理解)jar。 https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency