如何在进行 android espresso 测试时解决此 CalledFromWrongThreadException?
How to solve this CalledFromWrongThreadException when doing android espresso test?
我在做浓缩咖啡测试时出错
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891)
at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5751)
at android.view.View.playSoundEffect(View.java:20321)
at android.view.View.performClick(View.java:5636)
at nz.salect.handset.auth.EntryActivityUITest.should_open_logDetails_activity_when_pressing_logDetails_button(EntryActivityUITest.kt:47)
第47行是第二次测试中尝试执行点击操作的代码。
第一个测试通过,而第二个测试失败,因为它甚至不能 运行 由于异常。
@LargeTest
@RunWith(AndroidJUnit4::class)
class EntryActivityUITest {
@Rule
@JvmField
val entryAuthActivityRule = ActivityTestRule(EntryActivity::class.java, true, false)
@Test
fun it_should_show_logDetails_buttons_after_starting() {
entryAuthActivityRule.launchActivity(null)
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
@Test
fun should_open_logDetails_activity_after_pressing_logDetails_button() {
entryAuthActivityRule.launchActivity(null)
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
Espresso.onView(ViewMatchers.withId(R.id.loginDetails_textEdit_password))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
这里发生了什么?以及如何解决?
这两行
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
看起来很可疑。我会期待
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails)).perform(ViewActions.click())
我在做浓缩咖啡测试时出错
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891) at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5751) at android.view.View.playSoundEffect(View.java:20321) at android.view.View.performClick(View.java:5636) at nz.salect.handset.auth.EntryActivityUITest.should_open_logDetails_activity_when_pressing_logDetails_button(EntryActivityUITest.kt:47)
第47行是第二次测试中尝试执行点击操作的代码。
第一个测试通过,而第二个测试失败,因为它甚至不能 运行 由于异常。
@LargeTest
@RunWith(AndroidJUnit4::class)
class EntryActivityUITest {
@Rule
@JvmField
val entryAuthActivityRule = ActivityTestRule(EntryActivity::class.java, true, false)
@Test
fun it_should_show_logDetails_buttons_after_starting() {
entryAuthActivityRule.launchActivity(null)
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
@Test
fun should_open_logDetails_activity_after_pressing_logDetails_button() {
entryAuthActivityRule.launchActivity(null)
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
Espresso.onView(ViewMatchers.withId(R.id.loginDetails_textEdit_password))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
这里发生了什么?以及如何解决?
这两行
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
看起来很可疑。我会期待
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails)).perform(ViewActions.click())