Show navigation Hide navigation

Creating Unit Tests

This lesson teaches you to

  1. Create a Test Case for Activity Unit Testing
  2. Validate Launch of Another Activity

Try it out

Download the demo

AndroidTestingFun.zip

An Activity unit test is an excellent way to quickly verify the state of an Activity and its interactions with other components in isolation (that is, disconnected from the rest of the system). A unit test generally tests the smallest possible unit of code (which could be a method, class, or component), without dependencies on system or network resources. For example, you can write a unit test to check that an Activity has the correct layout or that it triggers an Intent object correctly.

Unit tests are generally not suitable for testing complex UI interaction events with the system. Instead, you should use the ActivityInstrumentationTestCase2 class, as described in Testing UI Components.

This lesson shows how you can write a unit test to verify that an Intent is triggered to launch another Activity. Since the test runs in an isolated environment, the Intent is not actually sent to the Android system, but you can inspect that the Intent object's payload data is accurate.

For a complete test case example, take a look at LaunchActivityTest.java in the sample app.

Note: To test against system or external dependencies, you can use mock objects from a mocking framework and inject them into your unit tests. To learn more about the mocking framework provided by Android, see Mock Object Classes.

Create a Test Case for Activity Unit Testing

The ActivityUnitTestCase class provides support for isolated testing of a single Activity. To create a unit test for your Activity, your test class should extend ActivityUnitTestCase.

The Activity in an ActivityUnitTestCase is not automatically started by Android Instrumentation. To start the Activity in isolation, you need to explicitly call the startActivity() method, and pass in the Intent to launch your target Activity.

For example:

public class LaunchActivityTest
        extends ActivityUnitTestCase<LaunchActivity> {
    ...

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mLaunchIntent = new Intent(getInstrumentation()
                .getTargetContext(), LaunchActivity.class);
        startActivity(mLaunchIntent, null, null);
        final Button launchNextButton =
                (Button) getActivity()
                .findViewById(R.id.launch_next_activity_button);
    }
}

Validate Launch of Another Activity

Your unit testing goals might include:

  • Verifying that LaunchActivity fires an Intent when a button is pushed clicked.
  • Verifying that the launched Intent contains the correct payload data.

To verify if an Intent was triggered following the Button click, you can use the getStartedActivityIntent() method. By using assertion methods, you can verify that the returned Intent is not null, and that it contains the expected string value to launch the next Activity. If both assertions evaluate to true, you've successfully verified that the Intent was correctly sent by your Activity.

You might implement your test method like this:

@MediumTest
public void testNextActivityWasLaunchedWithIntent() {
    startActivity(mLaunchIntent, null, null);
    final Button launchNextButton =
            (Button) getActivity()
            .findViewById(R.id.launch_next_activity_button);
    launchNextButton.performClick();

    final Intent launchIntent = getStartedActivityIntent();
    assertNotNull("Intent was null", launchIntent);
    assertTrue(isFinishCalled());

    final String payload =
            launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
    assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD, payload);
}

Because LaunchActivity runs in isolation, you cannot use the TouchUtils library to manipulate UI controls. To directly click a Button, you can call the performClick() method instead.