找不到活动。您是否忘了通过调用getActivity()或startActivitySync或类似方法来启动活动

时间:2019-04-06 13:22:11

标签: android android-espresso

启动我的Android应用时,请先启动FirstActivity。 当我单击FirstActivity上的按钮而不是启动AddTraderActivity时。 如果我按AddTraderActivity中的“ START REQUEST ”按钮,则会呼叫:

setResult(RESULT_OK);
finish();

结果AddTraderActivity被销毁并显示FirstActivity。 很好。

现在,我想为AddTraderActivityTest编写Espresso的测试。 在这里测试:

@RunWith(AndroidJUnit4::class)
@SmallTest
class AddTraderActivityTest {

    @get:Rule
    var addTraderActivity: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)

    @Test
    fun toolBarHeight() {
        onView(withId(R.id.toolBar)).check(matches(withHeightResId(R.dimen.tool_bar_height)))
    }

  @Test
    fun buttonStartTextUppercase() {
        onView(withId(R.id.startButton)).check(matches(withTextUppercaseResId(R.string.start)))
}

结果是,当我开始此测试时,仅启动AddTraderActivity并测试成功通过。 很好。

现在,我要为单击按钮“ START REQUEST

编写测试

这里测试:

@Test
    fun pressButtonStartProgressBarDisplayed() {
        onView(withId(R.id.baseTextInputEditText)).perform(typeText("BASE_TEST"))
        onView(withId(R.id.quoteTextInputEditText)).perform(typeText("QUOTE_TEST"))
        onView(withId(R.id.startButton)).perform(click())

        onView(withId(R.id.containerProgressBarLayout)).check(matches(isDisplayed()))
    }

测试运行并按“ START REQUEST ”按钮后,出现下一个错误:

测试从16:05开始...

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.AddTraderActivityTest#pressButtonStartProgressBarDisplayed' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:35)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:276)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我收到此错误是因为堆栈中没有活动FirstActivity,因为我的测试未启动它。 问题是。 我如何仅AddTraderActivity 隔离测试?

1 个答案:

答案 0 :(得分:0)

理想情况下,您应该使用单独的类来测试单独的活动。

但是,如果需要,您可以使用以下方法来隔离每种方法的规则

[编辑](警告:,您可以不再使用@get:Rule注释了)

@Test
public void myTest() {
      // launch desired activity
      var firstActivity: IntentsTestRule<FirstActivity> = IntentsTestRule(FirstActivity::class.java)
      firstActivity.launchActivity(Intent())

      // add tests
      onView(withId(R.id.baseTextInputEditText)).perform(typeText("BASE_TEST"))
      onView(withId(R.id.quoteTextInputEditText)).perform(typeText("QUOTE_TEST"))
}