Reputation: 1336
I recorded an Espresso test using the recorder in Android Studio 4.2.2, which included a single assertion that a text field on my MainActivity UI was showing with the correct text string. I then saved this to SplashActivityTest.java:
public class SplashActivityTest {
@Rule
public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);
@Before
public void registerIdlingResource() {
IdlingRegistry.getInstance().register(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@After
public void unregisterIdlingResource() {
IdlingRegistry.getInstance().unregister(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@Test
public void splashActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView.check(matches(isDisplayed()));
ViewInteraction textView2 = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView2.check(matches(withText("My Playlists")));
}
}
I added use of the Idling registry to this class because in my app what actually happens is a Splash screen is the launcher activity, which then launches the activity that loads the UI that I want to test.
I have this code:
// Necessary for automated tests, decrement handled in MainActivity.onResume()
CountingIdlingResourceSingleton.increment();
In the onCreate() method of SplashActivity and this code:
// Necessary for automated tests - increment is done in SplashActivity.onCreate()
CountingIdlingResourceSingleton.decrement();
At the end of onResume() in MainActivity.
The above code runs flawlessly, the test succeeds. Yay.
However, I get a deprecation warning on my use of ActivityTestRule, in favor of using an ActivityScenarioRule instead of ActivityTestRule (kind of interesting since use of that API was what was generated by the Espresso recorder in the latest 4.2.2 Android Studio, but that's the subject of a different post!).
So I change it:
public class SplashActivityTest {
@Rule
public ActivityScenarioRule<SplashActivity> mActivityTestRule = new ActivityScenarioRule<>(SplashActivity.class);
@Before
public void registerIdlingResource() {
IdlingRegistry.getInstance().register(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@After
public void unregisterIdlingResource() {
IdlingRegistry.getInstance().unregister(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@Test
public void splashActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView.check(matches(isDisplayed()));
ViewInteraction textView2 = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView2.check(matches(withText("My Playlists")));
}
}
Now it no longer runs flawlessly. My app starts, the application class runs, but the launcher class never is called. Instead I get:
java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, CREATED, STARTED, RESUMED]" (last lifecycle transition = "PRE_ON_CREATE")
Why? What do I need to do differently to insure that my normal launcher activity is called?
Upvotes: 5
Views: 3048