-1

I have this below code.I want to write junit test for this method.

@Override
public void getSuccessData(Response response) {
    if(response.getStatus().equalsIgnoreCase("success")){
        BaseApplication.getInstance().setAccessToken(response.getToken().getAccessToken());
        commonNavigate.navigateToHomeScreen((HomeActivity)view);
    }
}

How can i write junit test case for this method.I am very new to junit.

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
Ravi kumar
  • 41
  • 1
  • 2
  • 9

2 Answers2

0

This is (most probably) a callback method you want to test.

If you want to test a callback, you would need to understand mocking.

In very basic terms, mocking lets you create a fake source object and invoke some request method on it, and then verify that a particular callback has been invoked with certain parameters. Read about Mockito, which can be easily integrated with Android Studio: http://site.mockito.org/

Secondly, you code calls android-specific code:

BaseApplication.getInstance().setAccessToken(response.getToken().getAccessToken());
            commonNavigate.navigateToHomeScreen((HomeActivity)view);

This code has dependency upon Context object. Please read what Context object means in Android and how it is shared in Application/Activity/View classes. "navigateToHomeScreen" method surely needs a Context!

Either you will mock android dependencies with fake objects, or you could run Instrumented tests which provide Context and other Android-framework-defendant objects.

To sum up - these are wide and complex topics and you should make a research on them first.

Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19
0

Use Mockito framework if you want to test methods. You need to mock objects so that you can test the method with dummy response.

Please refer this link for mockito

https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#setup