page.title=Creating Unit Tests trainingnavtop=true @jd:body

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 {@link android.app.Activity} unit test is an excellent way to quickly verify the state of an {@link android.app.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 {@link android.app.Activity} has the correct layout or that it triggers an {@link android.content.Intent} object correctly.

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

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

For a complete test case example, take a look at {@code 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 {@link android.test.ActivityUnitTestCase} class provides support for isolated testing of a single {@link android.app.Activity}. To create a unit test for your {@link android.app.Activity}, your test class should extend {@link android.test.ActivityUnitTestCase}.

The {@link android.app.Activity} in an {@link android.test.ActivityUnitTestCase} is not automatically started by Android Instrumentation. To start the {@link android.app.Activity} in isolation, you need to explicitly call the {@link android.test.ActivityUnitTestCase#startActivity(android.content.Intent, android.os.Bundle, java.lang.Object) startActivity()} method, and pass in the {@link android.content.Intent} to launch your target {@link android.app.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:

To verify if an {@link android.content.Intent} was triggered following the {@link android.widget.Button} click, you can use the {@link android.test.ActivityUnitTestCase#getStartedActivityIntent()} method. By using assertion methods, you can verify that the returned {@link android.content.Intent} is not null, and that it contains the expected string value to launch the next {@link android.app.Activity}. If both assertions evaluate to {@code true}, you've successfully verified that the {@link android.content.Intent} was correctly sent by your {@link android.app.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 {@code LaunchActivity} runs in isolation, you cannot use the {@link android.test.TouchUtils} library to manipulate UI controls. To directly click a {@link android.widget.Button}, you can call the {@link android.view.View#performClick()} method instead.