How to check expected intent sent without actually launching activity in Espresso?

前端 未结 3 1506
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 01:42

I have a UI test which clicks a button, and then launch a new Activity in its onClickListener. The test checks whether expected intent is sent or not.

My problem is, I w

3条回答
  •  渐次进展
    2021-02-14 02:13

    If you want to test whether expected intent is sent without actually launching the activity you can do it by capturing the intent with an activityResult and then catching the activity :

    Intent intent = new Intent();
    ActivityResult intentResult = new ActivityResult(Activity.RESULT_OK,intent);
    
    intending(anyIntent()).respondWith(intentResult);
    
    onView(withId(R.id.view_id_to_perform_clicking)).check(matches(isDisplayed())).perform(click());
    
    intended(allOf(hasComponent(ActivityToBeOpened.class.getName())));
    

    This would catch any attempt of launching ActivityToBeOpened. If you want to be more specific you can also catch an intent with Extras:

    intended(allOf(hasComponent(ActivityToBeOpened.class.getName()), hasExtra("paramName", "value")));
    

    Hope that helps.

提交回复
热议问题