Eselfar
Eselfar

Reputation: 3869

NullPointerException on Unit Test

I'm testing a method on a class (a Presenter). The test used to work fine. Since then, I haven't changed anything on this part, neither in the gradle nor Android Studio.

My code is like that:

In the test

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 
    }
}

In the Presenter

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

    // ...
}

The Exception happens when I try to get the the CustomObj from the list, like if the list was null, when it's supposed to be mocked.

If I put a breakpoint on CustomObj obj = mPresenter.onCustomObjSelected(0, mCustomObjs); I can see that the list is correctly mocked.

Any idea why?

EDIT

Here is the error log

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

Upvotes: 1

Views: 11357

Answers (1)

EJK
EJK

Reputation: 12527

The problem appears to be that you have mocked the behavior (using when) but not the creation of the object mCustomObjs. You need to call Mockito.mock to create the 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());
}

Upvotes: 1

Related Questions