0

Writing unit test cases for my android application.Currently using the MVP architecture I have conditions to check the network connection but when I run the test case network info(isConnectedToNetwork) is returning the null value. Test case class is mentioned below.Using the Roboelectric framework for testing.

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) {

}

}

And inside my presenter implementer, the code is shown below.Also, How can get the activity instance properly and how can mock the Kinvey client in my test cases? Newbie to writing the test cases.Any help would be much appreciated.

 @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() {
BAbhilash
  • 1
  • 5

1 Answers1

0

kinvey doesn't have any special requirement about mocking their library. You can use Mockito or do it manually like any other third party dependancies.