dimanche 22 février 2015

Robolectric test fails while it should succeed

I am making an Android application that uses Retrofit as a Rest client and Robolectric for testing. I am new to Android testing and I am stuck on something for a while now. I have a login activity with 2 edittext, a button and a label (TextView for possible errors). All of my tests are working perfectly, but when I test things that happen after the callback then Robolectric doesn't see that somehow..


Example of a test that succeed (before callback):



@Test
@Config(qualifiers = "values")
public void emptyPasswordShouldShowError() throws Exception
{
txtUsername.setText("testuser1@kdg.be");
btnLogin.performClick();
ANDROID.assertThat(txtPassword).hasError("The password is empty");
}


(Oh and I am also using Fest for testing: http://ift.tt/1JzsMko).


Example of a test that fails (after callback):



@Test
@Config(qualifiers = "values")
public void wrongLoginShouldShowError() throws Exception
{
txtUsername.setText("wronguser@wronguser.be");
txtPassword.setText("wrongpassword");
btnLogin.performClick();
ANDROID.assertThat(lblErrorMessage).hasText("Wrong login!");
}


I've also tested it with toasts and other things but they all fail!


This is the function that goes off when you click on the login-button:



private void loginUser() {
final String username = txtUsername.getText().toString();
final String password = txtPassword.getText().toString();
if(!checkForErrors(username, password)) {
User user = new User(username, password);
api.loginUser(user, new Callback<User>() {
@Override
public void success(User user, Response response) {
SharedPreferences sharedPreferences = getSharedPreferences("session", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String sessionId = getCookie(response.getHeaders());
editor.putString("sessionId", sessionId);
editor.putString("username", username);
editor.apply();
startDashboardActivity();
}

@Override
public void failure(RetrofitError retrofitError) {
lblErrorMessage.setText(getString(R.string.error_wrong_login));
}
});
}
}


If I test it myself on my Emulator it works perfectly! But when I test it using Robolectric I always get this error: java.lang.AssertionError: Expected text "Wrong login!" but was <>.


Here is a screenshot of my app with the working error


I have no idea why this test fails, I hope you can help me out!


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire