androidmockitomvpandroid-testingandroid-mvp

NullPointerException when trying to test a void method in Presenter using Mockito


I'm trying to test my app with Mockito. It is built using MVP pattern. This is my Contract:

public interface CitiesContract {
    interface View {
        void addCitiesToList(List<City> cityList);
    }

    interface Presenter {
        void passCityListToView();
    }

    interface Model {
        List<City> getCityList();
    }
}

This is my View:

public class CitiesActivity extends AppCompatActivity implements CitiesContract.View {
    private List<City> cityList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

        CitiesPresenter presenter = new CitiesPresenter(this);
        presenter.passCityListToView();
    }

    @Override
    public void addCitiesToList(List<City> cities) {
        cityList.addAll(cities);
    }
}

This is my Presenter:

public class CitiesPresenter implements CitiesContract.Presenter {
    private CitiesContract.View view;
    private CitiesModel model;

    public CitiesPresenter(CitiesContract.View view) {
        this.view = view;
        model = new CitiesModel();
    }

    @Override
    public void passCityListToView() {
        List<City> cityList = model.getCityList();
        view.addCitiesToList(cityList);
    }
}

This is my Model:

public class CitiesModel implements CitiesContract.Model {
    @Override
    public List<City> getCityList() {
        List<City> cityList = new ArrayList<>();
        //Add 30 cities to the list
        return cityList;
    }
}

How can I test the passCityListToView() method within my Presenter? This is what I have tried so far:

public class CitiesPresenterTest {
    private CitiesContract.Presenter citiesPresenter;
    @Mock
    private CitiesContract.View citiesView;
    @Mock
    private CitiesContract.Model citiesModel;

    public void setUp() {
        MockitoAnnotations.initMocks(this);
        citiesPresenter = new CitiesPresenter(citiesView);
        citiesModel = new CitiesModel();
    }

    @Test
    public void testCityListIfNull() {
        when(citiesModel.getCityList()).thenReturn(null);
        citiesPresenter.passCityListToView();
        verify(citiesView).addCitiesToList(null);
    }
}

But I get NullPointerException pointing to this line:

when(citiesModel.getCityList()).thenReturn(null);

How can I successfully pass this test? Thanks in advance.

Edit:

As requested, this is my logcat:

java.lang.NullPointerException
    at com.example.CitiesPresenterTest.testCityListIfNull(CitiesPresenterTest.java:28)
    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.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.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:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Solution

  • Multiple things wrong.

    First:

    @Mock
    private CitiesContract.Model citiesModel;
    

    that is basically undone by:

    citiesModel = new CitiesModel();
    

    The annotation alone is enough to create a mocked object (to be precise: the annotation together with that initMocks() call).

    But you then come in and create a real object, which you then try to add a specification for. But when this here is invoked:

    when(citiesModel.getCityList()).thenReturn(null);
    

    as said, citiesModel isn't a Mockito created mock, but a real object of your production class.

    That can't work. So, start by removing citiesModel = new CitiesModel(); from your code.

    Next:

    You do model = new CitiesModel(); in your presenter class. But there is no magic that would somehow put your mocked model instance into that field of the presenter class.

    In other words: just declaring a mock alone isn't sufficient. You have to insure that it gets injected into your class under test. Either by passing it using a "test only" constructor (that takes more arguments) or by using the @InjectMocks annotation.