mardi 9 juin 2015

How to unit test an async interactor?

I am completely new in junit testing, and I need to test a interactor with some async calls. My problem is that I do not understand what do I need to test without breaking the unity of the test.

If I test a pure call to interactor.execute(), I am testing other interactors functionalities too. I could mock interactor dependencies but they are inyected. I know it is a conceptual misunderstood, it must be much easier than I think.

public class NewLoginInteractorImpl implements NewLoginInteractor {

    private final GetRegistrationIdInteractor getRegistrationIdInteractor;
    private final GetDeviceIdInteractor getDeviceIdInteractor;
    private final GetAndroidOSVersionInteractor getAndroidOSVersionInteractor;
    private final NewLoginWebService newLoginWebService;
    private static final String TAG = "NewLoginInteractorImpl";


    @Inject
    public NewLoginInteractorImpl(GetRegistrationIdInteractor getRegistrationIdInteractor, GetDeviceIdInteractor getDeviceIdInteractor, GetAndroidOSVersionInteractor getAndroidOSVersionInteractor, NewLoginWebService newLoginWebService) {
        this.getRegistrationIdInteractor = getRegistrationIdInteractor;
        this.getDeviceIdInteractor = getDeviceIdInteractor;
        this.getAndroidOSVersionInteractor = getAndroidOSVersionInteractor;
        this.newLoginWebService = newLoginWebService;
    }

    @Override
    public void execute(final Callback callback) {
        final String deviceId = getDeviceIdInteractor.execute();
        final String os_version = getAndroidOSVersionInteractor.execute();
        getRegistrationIdInteractor.execute(new GetRegistrationIdInteractor.Callback() {
            @Override
            public void onSuccess(String regid) {
                NoLoginUser noLoginUser = new NoLoginUser(deviceId, regid, os_version);
                callNoLoginWebService(noLoginUser, callback);
            }

            @Override
            public void onFailure(String error) {
                callback.onFailureGettingRegistrationId(error);
            }

            @Override
            public void onRecoverableError(int resultCode, int playServicesResolutionRequest) {
                callback.onRecoverableError(resultCode, playServicesResolutionRequest);
            }
        });
    }

    private void callNoLoginWebService(NoLoginUser noLoginUser, final Callback callback) {

        newLoginWebService.noLogin(noLoginUser, new NewLoginWebService.Callback() {

            @Override
            public void onNoLoginFailure(String errorText) {
                callback.onNoLoginFailure(errorText);
            }

            @Override
            public void onNoLoginInvalidValues(JSONObject error) {
                callback.onFailureGettingRegistrationId("ERROR");
            }

            @Override
            public void onNoLoginSuccess(JSONObject response) {
                callback.onSuccess(response);
            }

            @Override
            public void onNullResponse() {
                callback.onNullResponse();
            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire