Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPE when calling MockitoAnnotations.initMocks() in AndroidTestCase

Trying to use mockito in my AndroidTestCase. I added the dependencies to the build.gradle:

final DEXMAKER_VERSION = '1.2'

dependencies {
    // ...
    androidTestCompile "com.google.dexmaker:dexmaker:${DEXMAKER_VERSION}"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:${DEXMAKER_VERSION}"
    androidTestCompile 'org.mockito:mockito-core:1.10.19'

}

The TestCase with the mockito initialization:

public class UsersListPresenterTest extends AndroidTestCase {

    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    public void testInitialize() throws Exception {

    }
}

But as soon as I add any attribute to the class, even before adding any annotation the test start to crash:

public class UsersListPresenterTest extends AndroidTestCase {

    String mockString;

    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    public void testInitialize() throws Exception {

    }
}

With the following stacktrace

java.lang.NullPointerException: Attempt to invoke virtual method 
    'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter(DexmakerMockMaker.java:80)
at com.google.dexmaker.mockito.DexmakerMockMaker.getHandler(DexmakerMockMaker.java:75)
at org.mockito.internal.util.MockUtil.isMockitoMock(MockUtil.java:74)
at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:66)
at org.mockito.internal.configuration.injection.scanner.MockScanner.isMockOrSpy(MockScanner.java:86)
at org.mockito.internal.configuration.injection.scanner.MockScanner.preparedMock(MockScanner.java:72)
at org.mockito.internal.configuration.injection.scanner.MockScanner.scan(MockScanner.java:61)
at org.mockito.internal.configuration.injection.scanner.MockScanner.addPreparedMocks(MockScanner.java:47)
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:56)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
at com.myproject.presentation.UsersListPresenterTest.setUp(UsersListPresenterTest.java:28)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

What am I doing wrong?

like image 243
Addev Avatar asked Jun 02 '15 07:06

Addev


3 Answers

You could try to replace

MockitoAnnotations.initMocks(this);

with this

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

It works for me. See ref here

like image 191
Lunf Avatar answered Nov 15 '22 18:11

Lunf


Its a bug in dexmaker for which I have submitted a fix: https://github.com/crittercism/dexmaker/pull/24

like image 22
Andy Piper Avatar answered Nov 15 '22 19:11

Andy Piper


For me the solution was to use the Method Mockito.mock() for each Mocking Object instead of using MockitoAnnotations.initMocks(this);

So for example:

public class HomePresenterTest {
    private Repository repository;
    private HomePresenter presenter;

    @Before
    public void before() {
        repository = mock(Respository.class);
        presenter = new HomePresenter(repository);
    }

    //Your tests
}
like image 25
Philipp Hofmann Avatar answered Nov 15 '22 20:11

Philipp Hofmann