如何解决从已弃用的 ActivityTestRule 更改为 ActivityScenarioRule 会破坏测试
我使用 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),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),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")
为什么?我需要做些什么不同的事情来确保我的正常启动器活动被调用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。