The AndroidJUnitRunner class is a JUnit test runner that lets you run JUnit 3- or JUnit 4-style test classes on Android devices, including those using the Espresso and UI Automator testing frameworks.

The test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results. This class replaces the InstrumentationTestRunner class, which only supports JUnit 3 tests.

This test runner supports several common testing tasks, including the following:

Write JUnit tests

The test runner is compatible with your JUnit 3 and JUnit 4 (up to JUnit 4.10) tests. However, you should avoid mixing JUnit 3 and JUnit 4 test code in the same package, as this might cause unexpected results. If you are creating an instrumented JUnit 4 test class to run on a device or emulator, your test class must be prefixed with the @RunWith(AndroidJUnit4.class) annotation.

The following code snippet shows how you might write an instrumented JUnit 4 test to validate that the changeText operation in the ChangeTextBehavior class works correctly:


class ChangeTextBehaviorTest {
    val stringToBeTyped = "Espresso"
    val activityRule = ActivityTestRule(

    @Test fun changeText_sameActivity() {
        // Type text and then press the button.
            .perform(typeText(stringToBeTyped), closeSoftKeyboard())

        // Check that the text was changed.


public class ChangeTextBehaviorTest {

    private static final String stringToBeTyped = "Espresso";

    public ActivityTestRule<MainActivity> activityRule =
            new ActivityTestRule<>(MainActivity.class);

    public void changeText_sameActivity() {
        // Type text and then press the button.
                .perform(typeText(stringToBeTyped), closeSoftKeyboard());

        // Check that the text was changed.

Use Android Test Orchestrator

When using AndroidJUnitRunner version 1.0 or higher, you have access to a tool called Android Test Orchestrator, which allows you to run each of your app's tests within its own invocation of Instrumentation.

Android Test Orchestrator offers the following benefits for your testing environment:

  • Minimal shared state. Each test runs in its own Instrumentation instance. Therefore, if your tests share app state, most of that shared state is removed from your device's CPU or memory after each test.

    To remove all shared state from your device's CPU and memory after each test, use the clearPackageData flag.

  • Crashes are isolated. Even if one test crashes, it takes down only its own instance of Instrumentation, so the other tests in your suite still run.

Both Android Studio and Firebase Test Lab have Android Test Orchestrator pre-installed, though you need to enable the feature in Android Studio.

If you use a different toolchain to test your app, however, you can still use Android Test Orchestrator by completing the following steps:

  1. Include the necessary packages in your app's build file.
  2. Enable Android Test Orchestrator from the command-line.

Enable from Gradle

To enable Android Test Orchestrator using the Gradle command-line tool, complete these steps:

  1. Add the following statements to your project's build.gradle file: