0

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)
alayor
  • 4,537
  • 6
  • 27
  • 47
Jay
  • 473
  • 1
  • 6
  • 19
  • check the null value coming from the database first check the null like if(userDB.getValueFromSqlite("role",1)!=null) { if ("tutor".equalsIgnoreCase(userDB.getValueFromSqlite("role",1))) view.userRole("tutor"); else view.userRole("student"); – param Jun 07 '17 at 11:13
  • Please read about [mcve]. We can't tell you why your test code throws an exception when you leave out the whole setup part of your unit test. You are only showing us parts of essential elements. Beyond that: read https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – GhostCat Jun 07 '17 at 11:14
  • I dont see you have understood my problem, may be my bad i havent explained in brief way.. The problem is i got a method in presenter that calls database and on responce it calls method on View. When i started testing presenters, this UserDB which is mocked one, doesnt allow me to do what i want and gives NullPointerException. – Jay Jun 07 '17 at 11:27
  • Regarding your question: PowerMock(ito) can do that. But the better solution is to use some form of dependency injection and make sure that your production code is using a *factory* that creates objects for it (instead of directly calling new). Have a look at https://www.youtube.com/playlist?list=PLD0011D00849E1B79 for example. And hint: make comments on answers. Chances are that this specific answer will be deleted sooner or later. So all comments would be lost. – GhostCat Jun 07 '17 at 11:58

1 Answers1

1

You just need to change your doReturn sentence from:

Mockito.doReturn("tutor").when(userDB).getValueFromSqlite("role",1)

to

Mockito.doReturn("tutor").when(userDB.getValueFromSqlite("role",1))

This way you would be mocking or recording the call to getValueFromSqlite method instead of calling it directly while specifying the mocked behavior.

alayor
  • 4,537
  • 6
  • 27
  • 47
  • i dont understand why both are different? – Jay Jun 09 '17 at 05:04
  • Basically in the first one you are passing an object, and in the second one you're passing a _method call_. `when` method needs to know which particular method are you mocking. This is basically the way you need to use mockito. – alayor Jun 09 '17 at 16:00