【问题标题】:How do I use an ActivityTestRule with @ClassRule in Kotlin?如何在 Kotlin 中将 ActivityTestRule 与 @ClassRule 一起使用?
【发布时间】:2019-04-23 16:23:06
【问题描述】:

我怀疑我的问题与this one 相同,但那里的代码不完整,我一定遗漏了什么。

这是我的测试类:

@RunWith(AndroidJUnit4::class)
class MyTest {

    companion object {

        @ClassRule
        @JvmField
        val activityTestRule = ActivityTestRule(MainActivity::class.java, true, true)
    }

    @Test
    fun verifySomething() {
        onView(withId(R.id.something)).perform(click())
    }
}

测试失败并出现以下错误:

java.lang.RuntimeException: No activities found. Did you t to launch the activity by calling getActivity() or startActivitySync or similar?
at android.support.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at android.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at android.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.proxyProvideRootView(ViewInteractionModule_ProvideRootViewFactory.java:35)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at android.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:218)
at android.support.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我在这里做错了什么?

【问题讨论】:

    标签: android kotlin android-espresso android-instrumentation android-junit


    【解决方案1】:

    你没有做错任何事。这是AndroidJUnit4 测试运行器的一个缺点,它不执行带有@ClassRule 注释的规则。您可以删除@RunWith 注释并查看正在执行但正在崩溃的规则。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多