我在junit测试中是全新的,我需要测试一些带有异步调用的交互器。我的问题是我不明白在不破坏测试统一性的情况下我需要测试什么。
如果我测试对interactor.execute()的纯调用,我也会测试其他的interactors功能。我可以模拟交互器依赖项,但它们被引入。我知道这是一个概念上的误解,它必须比我想象的容易得多。
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();
}
});
}
}