I am testing my presenter. For some reason, I got nullPointer on MainPresenter class. But I init it in before method. Please let me know what I init wrong. The main logic is to pass the Event object to the presenter and verify that method was called.
public class MainPresenterTest {
MainPresenter mainPresenter;
@Mock
MainView mainView;
@Mock
ApiDataRepository apiDataRepository;
@Before
public void initialize(){
MockitoAnnotations.initMocks(this);
mainPresenter = new MainPresenter(apiDataRepository, mainView);
}
@Test
public void inflateDataList() {
String errorMessage = "Error Message";
UnitApiEvent event = new UnitApiEvent(null,errorMessage);
mainPresenter.inflateDataList(event);
verify(mainView, times(1)).showError(errorMessage);
}
}
Error message
java.lang.NullPointerException
at com.rwapp.interviewapp.presenters.MainPresenter.inflateDataList(MainPresenter.java:35)
at com.rwapp.interviewapp.presenters.MainPresenterTest.inflateDataList(MainPresenterTest.java:40)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Presenter
public class MainPresenter {
ApiDataRepository mApiDataRepository;
MainView mMainView;
public MainPresenter(ApiDataRepository mApiDataRepository, MainView mMainView) {
this.mApiDataRepository = mApiDataRepository;
this.mMainView = mMainView;
}
/**
* Getting data for the list on the MainActivity
*/
public void getData() {
mApiDataRepository.getUnitsData();
}
/**
* Unpack the response from UnitApi call.
*/
@Subscribe
public void inflateDataList(UnitApiEvent event) {
if (event.getmData().size() > 0 && event.getmErrorMessage() == null) {
mMainView.inflateData(event.getmData());
} else {
mMainView.showError(event.getmErrorMessage());
}
}