I got a method in Presenter that calls a database. Now, when I try to test method that actually communicate with database, it gives me NullPointerException
.
How do we actually handle such cases?
public void getRoleFromSQLite(){
if ("tutor".equalsIgnoreCase(userDB.getValueFromSqlite("role",1)))
view.userRole("tutor");
else
view.userRole("student");
}
TEST:
@Test
public void getRoleFromSqliteDatabaseTest(){
Mockito.doReturn("tutor").when(userDB).getValueFromSqlite("role",1)
presenter.getRoleFromSQLite();
Mockito.verify(viewMock).userRole("tutor");
}
Exception:
java.lang.NullPointerException
at com.dolevel.level.db.UserDB.getValueFromSqlite(UserDB.java:130)
at com.dolevel.level.presenters.UserProfileScreenPresenter.getRoleFromSQLite(UserProfileScreenPresenter.java:28)
at com.dolevel.level.UserProfileScreenPresenterTest.getRoleFromSqliteDatabaseTest(UserProfileScreenPresenterTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)