>我看到了这个问题:Mockito + Dexmaker on Android
使用答案后,我遇到了另一个问题:java.lang.RuntimeException: java.lang.IllegalArgumentException: 优化的数据目录/sdcard 不属于当前用户。共享存储无法保护应用程序免受代码注入攻击。
我使用最新版本的mockito和dexmaker以及dexmaker-mockito。
我正在使用一个单独的测试项目。
完整的堆栈跟踪(testParse是我的测试函数):
java.lang.RuntimeException: java.lang.IllegalArgumentException: Optimized data directory /sdcard is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:388)
at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:252)
at com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:54)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:49)
at org.mockito.Mockito.mock(Mockito.java:1245)
at org.mockito.Mockito.mock(Mockito.java:1139)
at com.myproject.parser.JSONParserTest.testParse(JSONParserTest.java:29)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.IllegalArgumentException: Optimized data directory /sdcard is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.loadDex(DexFile.java:149)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:259)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:223)
at dalvik.system.DexPathList.<init>(DexPathList.java:106)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:46)
at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:382)
... 19 more
我在 http://code.google.com/p/dexmaker/issues/detail?id=2 上找到了解决方案
有效的方法是将以下行放入 setUp():
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
让我们希望尽快进行修复,而不是使用此解决方法。
//Depending on the type of test, you can also do this:
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());