Wednesday, 10 February 2016

How to Test Dialer Activity with Espresso Intent?

If you are testing an application which talks to other applications like camera or dialer then Espresso Intent will help you to do so. Espresso-Intents is an extension of Espresso which allows validation and stubbing of Intents sent out  by the application. As you are familiar with Espresso now then using Espresso Intent will be very easy for you instead of using ActivityTestRule use IntenntsTestRule as it allows you use intent APIs for functional UI testing.

Espresso Intents monitors all the intents which attempts to launch activities from the application under test and you can validate that intend is seen using intended API.You can also send response with the launched intent activity and you can use hamcrest matchers for your assertions.

Below is screen-shot of simple app on click it launches the dialer activity. I will explain how can write test case to test such activity with Espresso Intent.

Dialer Activity Testing

Test Dialer with Espresso

1. Setting up Intent is the first step but before that you should have Espresso Setup which I explain in this post. Open your build.gradle of your app and then add below line in dependencies and Sync.


 2. At the start of your test case use IntentTestRule as shown in the test case.

3. Declare variables for phone number and dialer package.

4. First line of code is used for intent stubbing to black external intents.

5. Write the steps to enter the phone number and click on call button.

6. The intended API is used to launch the activity ,send data with it and to validate that the intended activity is launched.

@LargeTestpublic class DialerIntentTest {

    @Rule    public IntentsTestRule<DialerActivity> mActivityRule = new IntentsTestRule<>(

    private static final String PHONE_NUMBER = "1234567890";
    private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + PHONE_NUMBER);
    private static String PACKAGE_ANDROID_DIALER = "";

    static {
            // Starting with Android Lollipop the dialer package has changed. 

            PACKAGE_ANDROID_DIALER = "";

    @Test    public void testDialerIntent()throws Exception
        intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));

I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)


  1. java.lang.NullPointerException: Attempt to invoke virtual method '' on a null object reference


  2. java.lang.NullPointerException: Attempt to invoke virtual method '' on a null object reference