Android模拟器上的Mockito

时间:2013-03-25 14:36:35

标签: android testing mockito dalvik dex

Android新手试图在Android中使用我最喜欢的Java测试工具。我试图使用Mockito 1.9.5,如以下博客文章中所述,但无法让测试在我的模拟器上运行(我目前没有物理设备可以测试)。

Mockit-Android教程:http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/

enter image description here

我可以毫无问题地执行所有正常的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的依赖......

https://fisheye2.atlassian.com/browse/mockito/trunk/cglib-and-asm/src/org/mockito/cglib/transform/AbstractProcessTask.java?r=1430

进一步调试,我再次被阻止:

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不在类路径上,但是我已经正确地将它添加到了test-project的libs目录中:

enter image description here

https://github.com/junit-team/junit/blob/r4.11/src/main/java/org/junit/internal/AssumptionViolatedException.java

请注意,就Mockito 1.9.5而言,我正在使用Junit-4.11的正确(根据我的理解)罐子。 https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency

3 个答案:

答案 0 :(得分:0)

你必须确保Mockito的JAR是你的一部分APK依赖。

如果您使用的是Intellij,请打开项目设置和Module-> Dependencies,并将Mockito添加为依赖项的一部分。

如果你正在使用混淆,你还必须确保它不会从你构建的apk中混淆mockito的类。

答案 1 :(得分:0)

我在调用原生API时遇到了同样的问题而没有嘲笑它们 - 确保你有一个原生调用的模拟。 还要检查链接错误,而不仅仅是Mockito异常。

答案 2 :(得分:0)

此问题可能是由于您的实际Android设备上没有库,即使您可能已将它们添加到Eclipse或Android Studio中。

在我的android测试项目中,我能够通过创建一个libs文件夹并将三个必需的库放入其中来解决这个问题。确保您创建的文件夹的名称实际上是“libs”而不是“lib”

这是android开发者网站的链接,其中包含有关添加支持库的更详细说明。 http://developer.android.com/tools/support-library/setup.html