vendredi 27 septembre 2019

How to test multiple service calls

I'm trying to increase my code coverage on Android. But I can't find the correct way to test this presenter. The onSelectContact makes a service call and later my ServiceFactory.getContactService makes another. How can I mock this calls?

public class ContactListPresenter {

    public void onSelectContact(User user) {
        getCorrespondingContactCall(user).enqueue(new Callback <JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                switch (response.code()) {
                    case case1:
                        goToFirstActivity(user, response);
                        break;
                    case case2:
                        goToSecondActivity(user, response);
                        break;
                    default: showInvalidInput(user);
                        break;
                }
            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable throwable) {
                if (getView() != null) {
                    getView().showErrorView();
                }
            }
        });
    }

    protected Call<JsonElement> getCorrespondingContactCall(final User user) {
        return StringUtils.isValidEmail(user.getEmail())
                ? ServiceFactory.getContactService().checkContactByEmail(user.getEmail())
                : ServiceFactory.getContactService().checkContactByPhoneNumber(user.getPhoneNumber());
    }     

}

Aucun commentaire:

Enregistrer un commentaire