启动另一个检查时失败 activity:

Fail when check is start another activity:

单击 TradersActivity 上的按钮时开始 JsonViewActivity

我需要检查是否成功启动 JsonViewActivity。这里是 Espresso 测试:

@RunWith(AndroidJUnit4::class)
class TradersActivityTest {

    val context = InstrumentationRegistry.getInstrumentation().getContext()
    val targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext()


    @Rule
    @JvmField
    var tradersActivitytRule = ActivityTestRule(TradersActivity::class.java)

  @Test
    fun itemList_viewJsonButton_click_check() {
        //scroll
        onView(withId(R.id.tradersRecyclerView))
                .perform(scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
        // click
        onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.viewJsonButton))
                .perform(click())
        // check is start JsonViewActivity activity
        intended(hasComponent(JsonViewActivity::class.java.getName()))
    }

但是测试失败:

已开始 运行 次测试

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.test.espresso.intent.Intents.internalIntended(org.hamcrest.Matcher, androidx.test.espresso.intent.VerificationMode, java.util.List)' on a null object reference
at androidx.test.espresso.intent.Intents.check(Intents.java:194)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:419)
at androidx.test.espresso.ViewInteraction.call(ViewInteraction.java:282)
at androidx.test.espresso.ViewInteraction.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)

您应该使用 IntentsTestRule 而不是 ActivityTestRule。