我正在尝试在我的 Android 项目中使用 Mockito.我找到了处理它的非常好的教程:http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/
I am trying to use Mockito in my Android project. I have found very nice tutorial that deals with it: http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/
基本上它使用新版本的 Mockito + Dexmaker 并且一切都按预期工作.
但是,当我尝试模拟一些 Android 特定对象时,即:
Basically it uses new version of Mockito + Dexmaker and everything works as expected.
However, when I try to mock some Android specific object i.e:
Context context = mock(Context.class);
我收到此异常:
java.lang.IllegalArgumentException:
dexcache == null (and no default could be found;
consider setting the 'dexmaker.dexcache' system property)
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:359)
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)
知道怎么解决吗?
从@rjath 对@MrChaz 答案的评论来看,这对我来说效果更好:
From @rjath's comment of @MrChaz's answer, this works better for me:
System.setProperty(
"dexmaker.dexcache",
getInstrumentation().getTargetContext().getCacheDir().getPath());
我把它放在我的 setUp()
方法中.
I put it in my setUp()
method.
这篇关于Android 上的 Mockito + Dexmaker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!