Android新手試圖在Android中使用我最喜歡的Java測試工具。我試圖使用Mockito 1.9.5,如以下博客文章所述,但無法讓測試在我的模擬器上運行(我目前沒有任何物理設備可以測試)。Android模擬器上的Mockito
Mockit-的Android教程:http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/
我能夠但是執行沒有問題,我所有的正常的JUnit測試任何我已經利用的Mockito的測試,因爲我收到以下:
Can't open dex cache '/data/dalvik-cache/[email protected]@[email protected]@[email protected]': No such file or directory
Unable to open or create cache for /data/data/com.trendium.peg/cache/Generated-621101.jar (/data/dalvik-cache/[email protected]@[email protected]@[email protected])
failed: testStartable(com.trendium.peg.services.RatingServiceTest)
java.lang.AssertionError: java.lang.ClassNotFoundException: RemoteRestTask_Proxy in loader [email protected]
我已經做了大量的谷歌搜索這些例外,重建我的模擬器,重建項目和其他各種想法,但沒有解決這個問題有任何運氣。
附註:我針對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的任務。不過,我沒有看到如螞蟻的的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-核心罐子是不是在classpath,但是我已經正確添加它的測試項目的libs目錄:
請注意,對於Mockito 1.9.5,我使用的是正確的(來自我的理解)JUNIT-4.11的罐子。 https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency
是。你應該用mockito-all替換mockito-core。 – 2013-03-26 00:24:58
@JesseWilson我剛開始時只是在libs文件夾中使用mockito-all-1.9.5 jar。上面的捕獲是在JUnit 4.11中放入以填充一些不滿意的依賴關係,並遵循上面的DeclaringMockitoDependency鏈接提供的建議。 – 2013-03-26 03:01:11
@ wangyif2我對proguard沒有做任何事情,所以我期望不會出現混淆。 – 2013-03-26 03:20:17