3

我使用 Android Studio 4.2.2 中的记录器记录了 Espresso 测试,其中包括一个断言,即我的 MainActivity UI 上的一个文本字段正在显示正确的文本字符串。然后我将其保存到 SplashActivityTest.java:

公共类 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")));
}

}

我在此类中添加了 Idling 注册表的使用,因为在我的应用程序中实际发生的是启动器活动,启动器活动然后启动加载我想要测试的 UI 的活动。

我有这个代码:

    // Necessary for automated tests, decrement handled in MainActivity.onResume()
    CountingIdlingResourceSingleton.increment();

在 SplashActivity 的 onCreate() 方法和这段代码中:

    // Necessary for automated tests - increment is done in SplashActivity.onCreate()
    CountingIdlingResourceSingleton.decrement();

在 MainActivity 中的 onResume() 结束时。

上面的代码运行完美,测试成功。耶。

但是,我在使用 ActivityTestRule 时收到了弃用警告,赞成使用 ActivityScenarioRule 而不是 ActivityTestRule(有点有趣,因为该 API 的使用是由最新的 4.2.2 Android Studio 中的 Espresso 记录器生成的,但仅此而已不同帖子的主题!)。

所以我改变它:

公共类 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")));
}

}

现在它不再完美运行。我的应用程序启动,应用程序类运行,但启动器类从未被调用。相反,我得到:

java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, CREATED, STARTED, RESUMED]" (last lifecycle transition = "PRE_ON_CREATE")

为什么?我需要做些什么不同的事情来确保调用我的正常启动器活动?

4

0 回答 0