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)
4
  • 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"); Commented Jun 7, 2017 at 11:13
  • Please read about minimal reproducible example. 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 stackoverflow.com/questions/218384/… Commented Jun 7, 2017 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. Commented Jun 7, 2017 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 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. Commented Jun 7, 2017 at 11:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i dont understand why both are different?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.