如何在Kotlin中将ActivityTestRule与@ClassRule一起使用?

时间:2019-04-23 16:23:06

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

我怀疑我的问题与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)

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

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