jeudi 23 juillet 2015

Android execution functional test order

I have an application where the MainActivity layout is made of two TextView (tvName and tvEmail) and one button (btnLogout).

I have a functional test (code at the end) with these test methods:

  • testUserDetailsAreShown: checks if the TextView contain the correct text values.
  • testLoginActivityStarts: performs a click action on btnLogout button and checks if the LoginActivity is loaded.

My problem is that if I comment the testLoginActivityStarts, then testUserDetailsAreShown pass. However, when both are uncommented then both fail.

java.lang.NullPointerException
at android.support.test.espresso.intent.Intents.resumedActivitiesExist(Intents.java:235)
at android.support.test.espresso.intent.Intents.intended(Intents.java:184)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at net.example.login.MainActivityTest.testLoginActivityStarts(MainActivityTest.java:46)

and

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: net.example.login:id/tvName

I think that this is because testLoginActivityStarts is executed before testUserDetailsAreShown and when testUserDetailsAreShown is under execution, the loaded activity is not MainActivity, but LoginActivity.

Can anyone help me with this? I need to make both tests pass. I'm sure that they should pass.

Here the test code:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private String USER_NAME = "UserName";
private String USER_EMAIL = "UserEmail";

public MainActivityTest(){
    super(MainActivity.class);
}

public void testUserDetailsAreShown() {
    onView(withId(R.id.tvName)).check(matches(withText(USER_NAME)));
    onView(withId(R.id.tvEmail)).check(matches(withText(USER_EMAIL)));
}

public void testLoginActivityStarts() {
    onView(withId(R.id.btnLogout)).perform(click());
    intended(hasComponent(LoginActivity.class.getName()));
}
}

Aucun commentaire:

Enregistrer un commentaire