15

Its been 2 weeks since I am studying Espresso and I could not grasp intending and intended. When do I use intending and intended? The provided example and online tutorials are not helping and researching the web is doing me more damage than good.

Semantically, for intellectual reference, why is it intend-ing and the other intended, this added more to the confusion. Is this another Google naming blunder or it's just me? These two methods really don't make sense.

I am misunderstanding its usage. I wanted to test if my activity A launched activity B. That's it. Here is my code:

@Test
public void shouldLaunchTagListActivity()
{
    onView(withId(R.id.edittext_description_minimized))
            .perform(click());

    onView(withId(R.id.linearlayout_add_note_maximize))
            .check(matches(isDisplayed()));

    onView(withId(R.id.relativelayout_quick_action_button))
            .check(matches(isDisplayed()));

    onView(withId(R.id.imagebutton_tag))
            .perform(click());

    // should I intended or intending here?
    // ???
    intended(toPackage(HomeScreenActivity.class.getName()));

    onView(withId(R.id.coordinatorlayout_tag_list))
            .check(matches(isDisplayed()));
}

This test always passes even I supplant the intent with the wrong the target.

I can check if the other activity has been launched by checking if my target view is existing and was seen by the user. But now I am going to run on a different user story where I really need to check if the activity sent the request to launched another activity (my activity, not external).

Any explanation is greatly appreciated!

1 Answer 1

35

The difference is Intended only verifies that an Intent was launched (what you want). Intending will return a result when the Intent is launched.

For Intended (if you want to check if the intent did actually launched your activity)

intended(hasComponent(TagListActivity.class.getName()));

This will fail if you press a button and launched, say, MyActivity, and you test intended for TagListActivity. It will throw this following error:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

Now, if you were testing that onActivityResult was handled properly, you would use Intending and pass in an ActivityResult similar to this:

Intent resultData = new Intent();
resultData.putExtra("resultData", "fancyData");
ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);

intending(toPackage(HomeScreenActivity.class.getName())).respondWith(result));

// Perform action that throws the Intent
onView(withId(R.id.imagebutton_tag)).perform(click());

// Verify your Activity is in the state it should be here.

So in this case, if you were expecting "HomeScreenActivity" to return something, you would use Intending.

3
  • 4
    @NeonWarge: To answer the semantics question, I believe it's because Intending doesn't actually verify, and you set it up before the Intent is thrown so the result will be returned. Where as Intended is verified after the Intent is thrown.
    – DeeV
    Commented Sep 27, 2016 at 14:49
  • Thank you, that is very clear. I will edit your answer to add something about intending I was able to make my test work now.
    – nww04
    Commented Sep 27, 2016 at 14:50
  • Please verify my edit, if it is wrong, ignore it and then correct my understanding if possible. Thanks!
    – nww04
    Commented Sep 27, 2016 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.