在AndroidTestCase中调用MockitoAnnotations.initMocks()时的NPE

时间:2015-06-02 07:57:23

标签: java android testing gradle mockito

尝试在我的AndroidTestCase中使用mockito。我将依赖项添加到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'

}

使用mockito初始化的TestCase:

public class UsersListPresenterTest extends AndroidTestCase {

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

    public void testInitialize() throws Exception {

    }
}

但是只要我向类中添加任何属性,即使在添加任何注释之前,测试也会开始崩溃:

public class UsersListPresenterTest extends AndroidTestCase {

    String mockString;

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

    public void testInitialize() throws Exception {

    }
}

使用以下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)

我做错了什么?

4 个答案:

答案 0 :(得分:8)

您可以尝试替换

var faculty = (Faculty)ViewBag.StudentFaculty

用这个

MockitoAnnotations.initMocks(this);

它对我有用。 See ref here

答案 1 :(得分:6)

这是我提交修复的dexmaker中的一个错误: https://github.com/crittercism/dexmaker/pull/24

答案 2 :(得分:2)

对我来说,解决方案是为每个Mocking对象使用方法Mockito.mock(),而不是使用MockitoAnnotations.initMocks(this);

例如:

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

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

    //Your tests
}

答案 3 :(得分:1)

我在那里创建了一个问题https://github.com/mockito/mockito/issues/392 带有修补程序的原始答案https://stackoverflow.com/a/36642606/1224247