RxJava中成功的单元测试 CreateAccountButtonClicked(GO99ApplicationConfigureApplications)上的公共void{ Log.i(标记“单击创建帐户按钮”); PasswordAndEmailValidator PasswordAndEmailValidator=新PasswordAndEmailValidator(); 如果(signUpView.getFirstName().isEmpty()| | signUpView.getLastName().isEmpty()| | signUpView.getEmailAddress().isEmpty()| | signUpView.getPassword().isEmpty()) ||signUpView.getConfirmPassword().isEmpty()){ signUpView.message(“字段为空”); 返回; } 如果(!passwordAndEmailValidator.validatePassword(signUpView.getPassword())){ signUpView.showErrorMessage(“密码长度必须至少为6个字符”); 返回; } 如果(!signUpView.getConfirmPassword().equals(signUpView.getPassword())){ signUpView.showErrorMessage(“密码不匹配”); 返回; } RequestGSON RequestGSON=新RequestGSON(); requestGSON.setEmail(signUpView.getEmailAddress().replaceAll(“\\s”,“”“).toLowerCase()); setFirstName(signUpView.getFirstName()); requestGSON.setLastName(signUpView.getLastName()); setPassword(signUpView.getPassword()); creationService.createUser(requestGSON).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleSubscriber()){ @凌驾 成功时公共无效(重新响应值){ 对象数据=value.getData(); 字符串dataString=gson.toJson(数据); CreatedUserObjectData CreatedUserObjectData=gson.fromJson(dataString,CreatedUserObjectData.class); signUpView.login(配置应用程序,createdUserObjectData.getUserId(),signUpView.getEmailAddress().toLowerCase().replaceAll(“\\s”,”),signUpView.getPassword()); } @凌驾 公共无效onError(可丢弃错误){ } }); }

RxJava中成功的单元测试 CreateAccountButtonClicked(GO99ApplicationConfigureApplications)上的公共void{ Log.i(标记“单击创建帐户按钮”); PasswordAndEmailValidator PasswordAndEmailValidator=新PasswordAndEmailValidator(); 如果(signUpView.getFirstName().isEmpty()| | signUpView.getLastName().isEmpty()| | signUpView.getEmailAddress().isEmpty()| | signUpView.getPassword().isEmpty()) ||signUpView.getConfirmPassword().isEmpty()){ signUpView.message(“字段为空”); 返回; } 如果(!passwordAndEmailValidator.validatePassword(signUpView.getPassword())){ signUpView.showErrorMessage(“密码长度必须至少为6个字符”); 返回; } 如果(!signUpView.getConfirmPassword().equals(signUpView.getPassword())){ signUpView.showErrorMessage(“密码不匹配”); 返回; } RequestGSON RequestGSON=新RequestGSON(); requestGSON.setEmail(signUpView.getEmailAddress().replaceAll(“\\s”,“”“).toLowerCase()); setFirstName(signUpView.getFirstName()); requestGSON.setLastName(signUpView.getLastName()); setPassword(signUpView.getPassword()); creationService.createUser(requestGSON).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleSubscriber()){ @凌驾 成功时公共无效(重新响应值){ 对象数据=value.getData(); 字符串dataString=gson.toJson(数据); CreatedUserObjectData CreatedUserObjectData=gson.fromJson(dataString,CreatedUserObjectData.class); signUpView.login(配置应用程序,createdUserObjectData.getUserId(),signUpView.getEmailAddress().toLowerCase().replaceAll(“\\s”,”),signUpView.getPassword()); } @凌驾 公共无效onError(可丢弃错误){ } }); },android,rx-java,rx-android,Android,Rx Java,Rx Android,我需要对这个方法进行单元测试。我用的是Mockito。在我使用RxJava时,请帮助我测试onSuccess()方法。有很多方法可以测试您的方法。但是,如果您怀疑如何测试RxJava部件,那么您可以使用TestSubscriber。你可以查一个例子 问题是,在OnCreateAccountButtonClick方法中有很多代码,它有不止一个职责。。。因此,在相同的方法中有很多测试。单独测试PasswordandMailValidator和creatingService是更好的方法 好吧,但你想测

我需要对这个方法进行单元测试。我用的是Mockito。在我使用RxJava时,请帮助我测试onSuccess()方法。

有很多方法可以测试您的方法。但是,如果您怀疑如何测试RxJava部件,那么您可以使用TestSubscriber。你可以查一个例子

问题是,在OnCreateAccountButtonClick方法中有很多代码,它有不止一个职责。。。因此,在相同的方法中有很多测试。单独测试PasswordandMailValidator和creatingService是更好的方法

好吧,但你想测试成功吗?

您可以这样做:

public void onCreateAccountButtonClicked(Go99ApplicationconfigureApplication) {
    Log.i(TAG, " On create account button clicked");

    PasswordAndEmailValidator passwordAndEmailValidator = new PasswordAndEmailValidator();
    if (signUpView.getFirstName().isEmpty() || signUpView.getLastName().isEmpty() || signUpView.getEmailAddress().isEmpty() || signUpView.getPassword().isEmpty()
            || signUpView.getConfirmPassword().isEmpty()) {
        signUpView.showErrorMessage("Field empty");
        return;
    }

    if (!passwordAndEmailValidator.validatePassword(signUpView.getPassword())) {
        signUpView.showErrorMessage("Password must be a at least 6 characters long.");
        return;
    }

    if (!signUpView.getConfirmPassword().equals(signUpView.getPassword())) {
        signUpView.showErrorMessage("Passwords don't match");
        return;
    }

    RequestGSON requestGSON = new RequestGSON();
    requestGSON.setEmail(signUpView.getEmailAddress().replaceAll("\\s", "").toLowerCase());
    requestGSON.setFirstName(signUpView.getFirstName());
    requestGSON.setLastName(signUpView.getLastName());
    requestGSON.setPassword(signUpView.getPassword());

    creationService.createUser(requestGSON).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleSubscriber<RestResponse>() {

        @Override
        public void onSuccess(RestResponse value) {
            Object data = value.getData();
            String dataString = gson.toJson(data);
            CreatedUserObjectData createdUserObjectData = gson.fromJson(dataString, CreatedUserObjectData.class);
            signUpView.login(configureApplication, createdUserObjectData.getUserId(), signUpView.getEmailAddress().toLowerCase().replaceAll("\\s", ""), signUpView.getPassword());

        }

        @Override
        public void onError(Throwable error) {

        }
    });

}
然后将这个类注入要测试的RxJava函数中。这样,您将始终处于同一线程中,因此您不会在没有响应的情况下完成测试

好的,现在是第二步

2->为您想要测试的视图注入一个模拟。这是注册视图,对吗?因此,像这样实例化您的类:

public class ImmediateSchedulerProvider {

    @NonNull
    @Override
    public Scheduler computation() {
        return Schedulers.immediate();
    }

    @NonNull
    @Override
    public Scheduler io() {
        return Schedulers.immediate();
    }

    @NonNull
    @Override
    public Scheduler ui() {
        return Schedulers.immediate();
    }
}
然后调用do您的测试并使用方法Mokito.verify(T)

3->

@Mock
SighUpView sighUpView

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
}
嗯,就是这样。以此为例,根据您的需要进行调整

如果您需要更完整的实现,您可以通过谷歌进行检查


快乐编码

需要验证SignUpView.login(…)是否被调用感谢回复。我想验证是否调用了以下代码:signUpView.login(configureApplication,CreatedUserObject.getUserId(),signUpView.getEmailAddress().toLowerCase().replaceAll(\\s',“”),signUpView.getPassword());如果你有什么建议的话,请告诉我是的,我有一个建议。检查我的编辑。我希望它能帮助你。
@Mock
SighUpView sighUpView

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
}
@Test
public void someTest(){

//Inject your mock ImeddiateScheculerProvider here or elsewhere. 
mSomeClass.onCreateAccountButtonClicked(mGo99ApplicationconfigureApplication, mImmediateSchudelerProvider.

//You must verify that your method was called with correct parameters!
Mockito.verify(sighUpView).login(configureApplication, createdUserObjectData.getUserId(), signUpView.getEmailAddress().toLowerCase().replaceAll("\\s", ""), signUpView.getPassword());
}