的Mockito + Dexmaker在AndroidMockito、Dexmaker、Android

2023-09-05 08:23:37 作者:待我兵戎马卸给你天下i

我想使用的Mockito在我的Andr​​oid项目。 我发现很不错的教程,它涉及: 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);

我收到此异常:

I receive this exception:

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)

不知道如何解决它?

Any idea how to fix it?

推荐答案

从@ MrChaz的回答@ rjath的评论,这能更好地工作:

From @rjath's comment of @MrChaz's answer, this works better for me:

System.setProperty(
    "dexmaker.dexcache",
    getInstrumentation().getTargetContext().getCacheDir().getPath());

我把它放在我的设置()方法。