当检查开始另一个活动时失败:

时间:2019-04-18 12:23:50

标签: android kotlin android-espresso ui-testing

TradersActivity上单击按钮时,开始JsonViewActivity

我需要检查是否成功开始JsonViewActivity。这里是浓缩咖啡测试:

@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$2.check(Intents.java:194)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:419)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:282)
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)

1 个答案:

答案 0 :(得分:1)

您应该使用IntentsTestRule而不是ActivityTestRule。