单元测试上的空指针异常



我正在类(演示器(上测试方法。测试曾经工作正常。从那以后,我在这个部分没有改变任何东西,无论是在gradle还是Android Studio中。

我的代码是这样的:

在测试中

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;

@RunWith(MockitoJUnitRunner.class)
public class MyPresenterTest {
    @Mock
    private CustomObj mCustomObj;
    @Mock
    private List<CustomObj> mCustomObjs;
    private MyPresenter mPresenter;
    @Before
    public void initTest() {
        // ...      
        Mockito.when(mCustomObjs.get(anyInt())).thenReturn(mCustomObj);
        mPresenter = Mockito.spy(new MyPresenter());
    }
    @Test
    public void testOnCustomObjSelected() throws Exception {
        CustomObj obj = mPresenter.onCustomObjSelected(0, mCustomObjs);
        // ... irrelevant code 
    }
}

在演示者中

void onCustomObjSelected(int pos, final List<CustomObj> customObjs) {
    CustomObj obj = customObjs.get(pos); // The Exception happens here
    // ...
}

当我尝试从列表中获取 CustomObj 时,就会发生异常,例如如果列表为空,当它应该被模拟时。

如果我在CustomObj obj = mPresenter.onCustomObjSelected(0, mCustomObjs);上放置断点,我可以看到列表被正确模拟。

知道为什么吗?

编辑

这是错误日志

java.lang.NullPointerException
    at com.example.MyPresenter.onCustomObjSelected(MyPresenter.java:160)
    at com.example.MyPresenterTest.testOnCustomObjSelected(MyPresenterTest.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Process finished with exit code -1

问题似乎是您模拟了行为(使用 when (,但没有模拟对象的创建mCustomObjs。 您需要调用Mockito.mock来创建模拟。

@Before
public void initTest() {
    // Add this line
    mCustomObjs = Mockito.mock(List.class);    
    Mockito.when(mCustomObjs.get(anyInt())).thenReturn(mCustomObj);
    mPresenter = Mockito.spy(new MyPresenter());
}

相关内容

  • 没有找到相关文章

最新更新