Kevin Schultz

Mobile Engineering

Mockito & Android How-To

| Comments

Mockito has become an essential part of my testing toolbox. Yet every single time I start a new Android project I have to hunt down the solution to the ‘dexcache == null’ error.

I haven’t founded a better solution that simply setting the System property as specified in the bug report. Thus, I have a simple class I add to every project’s test suite as a work around. It sets the dexmaker cache and tests the Mockito is property configured.

build.gradle
1
2
3
dependencies {
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
}
androidTest/java/…/MockitoSetupAndTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MockitoSetupAndTest extends AndroidTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
    }

    public void testMockito() {
        List mockedList = mock(List.class);

        mockedList.add("one");
        mockedList.clear();

        verify(mockedList).add("one");
        verify(mockedList).clear();
    }

}

Comments