如何在android中编写单元测试用例时获取Activity实例和模拟kinvey客户端

时间:2018-02-16 12:25:45

标签: android unit-testing mvp robolectric kinvey

为我的android应用程序编写单元测试用例。当前使用MVP架构我有条件检查网络连接,但是当我运行测试用例时,网络信息(isConnectedToNetwork)返回空值。测试用例类如下所述。使用Roboelectric框架进行测试。

public class LoginActivityTest implements LoginView {

private EditText mEtMail;
private AppCompatEditText mEtPassword;
private TextView mTvLoginLostPassword;
private Button mBtnLogin;
private LoginActivity loginActivity;
private LoginPresenter loginPresenter;
Client mKinveyClient;
private String errorString = "";
private Boolean isLoginSuccess = false;



@Before
public void setup() {
    loginActivity = Robolectric.buildActivity(LoginActivity.class).create().get();
    loginPresenter = new LoginPresenterImpl(this);
    mKinveyClient = AppConstant.mKinveyClient;
    mEtMail = (EditText) loginActivity.findViewById(R.id.email);
    mEtPassword = (AppCompatEditText) loginActivity.findViewById(R.id.pwd);
    mTvLoginLostPassword = (TextView) loginActivity.findViewById(R.id.forgotpassword);
    mBtnLogin = (Button) loginActivity.findViewById(R.id.login);
}

@Test
public void testOnPostLogin() {

    // Passing null email ID
    loginPresenter.performLogin(loginActivity, mKinveyClient, mEtMail.getText().toString(), mEtPassword.getText().toString());
    assertThat(errorString.equalsIgnoreCase("Please enter email address."));

    // Passing invalid email ID
    mEtMail.setText("kkdjakdjakkjk");
    loginPresenter.performLogin(loginActivity, mKinveyClient, mEtMail.getText().toString(), mEtPassword.getText().toString());
    assertThat(errorString.equalsIgnoreCase("Please enter valid email address."));

    // Passing null password
    mEtMail.setText("someID@gmail.com");
    loginPresenter.performLogin(loginActivity, mKinveyClient, mEtMail.getText().toString(), mEtPassword.getText().toString());
    assertThat(errorString.equalsIgnoreCase("Please enter password."));

    mTvLoginLostPassword.performClick();
    Intent expectedIntent = new Intent(loginActivity,ForgotPasswordActivity.class);
    Assert.assertEquals(
            shadowOf(loginActivity).getNextStartedActivity(),
            expectedIntent);
}



@Override
public void onError(String error) {
    errorString = error;
}

@Override
public void onLoginSuccess() {
    isLoginSuccess = true;
}

@Override
public void onErrorAlert(String errorAlert) {

}

}

在我的演示者实现者中,代码如下所示。另外,如何正确获取活动实例以及如何在我的测试用例中模拟Kinvey客户端?新手编写测试用例。非常感谢任何帮助。

 @Override
public void performLogin(final Activity activity, Client mKinveyClient, String username, String password) {
    if (Strings.isNullOrEmpty(username)) {
        mLoginView.onError("Please enter email address.");
    } else if (!isLoginRegexValid(username)) {
        mLoginView.onError("Please enter valid email address.");
    } else if (Strings.isNullOrEmpty(password)) {
        mLoginView.onError("Please enter password.");
    } else {
        if (isConnectedToNetwork(activity)) {
            if (AppConstant.mKinveyClient.user().isUserLoggedIn()) {
                AppConstant.mKinveyClient.user().logout().execute();
            }
            AppConstant.mKinveyClient.user().login(username.toLowerCase(), password, new KinveyUserCallback() {

1 个答案:

答案 0 :(得分:0)

kinvey对嘲笑他们的图书馆没有任何特殊要求。您可以像使用任何其他第三方依赖项一样使用Mockito或手动执行此操作。