How to unit test android app which requires a web service calls

Following are the tools and frameworks that I am using in my project .

  1. Retrofit
  2. Dagger 2
  3. Swagger
  4. Swagger retrofit client generator

I have also implemented MVP design pattern in my project .

I just want to know whether I need use real backend or Just need to mock the backend for testing purpose . If I need to mock the backend what is the recommenced tool

Do swagger provide any tool to mock web service

What are the best practice of Unit testing android (MVP) mainly presenter

I am posting it here because I didn't find any proper answer in web for all these questions

I have written a sample test case for my presenter please let me know if I done something wrong

Presenter

 public void doLogin(String userName, String password) {
        loginView.showProgressBar(true);
        AuthenticationcontrollerApi authService = mDataClient.createService(AuthenticationcontrollerApi.class);
        UserCredentials userCredentials = new UserCredentials();
        userCredentials.setUserName(userName);
        userCredentials.setPassword(password);
        authService.authorizeUsingPOST(userCredentials, new Callback<String>() {
            @Override
            public void success(String s, Response response) {
                loginView.showProgressBar(false);
                loginView.onLoginSuccess(true, s);
            }

            @Override
            public void failure(RetrofitError error) {
                loginView.showProgressBar(false);
                loginView.onLoginFailure(error);
            }
        });
    }

Test

@RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {

    @Mock
    protected AuthenticationcontrollerApi authService;

    @Mock
    protected DataClient mDataClient;

    @Mock
    LoginView loginView;


    private LoginPresenter loginPresenter;


    @Before
    public void setUp() {
        loginPresenter = new LoginPresenter(mDataClient);
        loginPresenter.attachView(loginView);
        when(mDataClient.createService(AuthenticationcontrollerApi.class)).thenReturn(authService);
    }

    @Test
    public void loginSuccessful() {
        String a = "";
        loginPresenter.doLogin("abc", "abc");
        verify(loginView).showProgressBar(true);
    }

    @After
    public void detachView() {
        loginPresenter.detachView();
    }
}

Thanks in advance

Answers


An unit test should test a "Unit". This mean that all of it dependencies should be explicitly controlled by you (eg mocked).

There is the integration tests which tests the integrated behaviour of different parts but that is a different topic.

Lets go through your questions:

I just want to know whether I need use real backend or Just need to mock the backend for testing purpose . If I need to mock the backend what is the recommenced tool

You don't need the real backend you are testing an Unit. You don't want your test to be affected by backend bugs.

Mockito is the recommended tool for this. You just mock the behaviour (What you started to do in your sample test).

Do swagger provide any tool to mock web service

I don't think you need anything special other then Mockito.

What are the best practice of Unit testing android (MVP) mainly presenter

In a presenter you mock out the views and the data sources (in your case the web services) and write your tests providing mock data for your test scenarios. In this way you test the business logic of the presenter, not the webservice.


Need Your Help

UILabel inside UITableViewCell - Cuts off information

ios objective-c cocoa-touch uitableview uilabel

I'm developing an iOS app to show the information about my facebook friends in a UITableView and I'm with a problem. I use a custom cell with 2 UILables (Field and description):

php plug-in for developers on firefox?

php firefox firefox-addon

Is there any plug-in for firefox that is usefull for PHP development?