Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito + Dexmaker on Android

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/

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?

like image 791
stevo.mit Avatar asked Sep 04 '12 16:09

stevo.mit


2 Answers

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

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

I put it in my setUp() method.

like image 187
Adil Hussain Avatar answered Oct 29 '22 20:10

Adil Hussain


I've managed to piece together a fix that seems to be working for me. To the manifest I added read and write external storage. To the test I added System.setProperty("dexmaker.dexcache", "/sdcard"); to the test. To the emulator image I added an SD card.

I believe this works because by default mockito tries to use the apps cache directory but I never run an activity so I suspect the directory is never created by the OS

like image 10
MrChaz Avatar answered Oct 29 '22 18:10

MrChaz