Google is committed to advancing racial equity for Black communities. See how.

JUnit4 rules with AndroidX Test

AndroidX Test includes a set of JUnit rules to be used with the AndroidJUnitRunner. JUnit rules provide more flexibility and reduce the boilerplate code required in tests.

ActivityScenarioRule

This rule provides functional testing of a single activity. The activity under test is launched before each test annotated with @Test and before any method annotated with @Before. It's terminated after the test is completed and all methods annotated with @After are finished. To access the activity under test in your test logic, provide a callback runnable to ActivityScenarioRule.getScenario().onActivity().

The following code snippet demonstrates how to incorporate ActivityScenarioRule into your testing logic:

Kotlin

@RunWith(AndroidJUnit4::class.java)
@LargeTest
class MyClassTest {
    @get:Rule
    val activityRule = ActivityScenarioRule(MyClass::class.java)

    @Test fun myClassMethod_ReturnsTrue() { ... }
}

Java

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyClassTest {
    @Rule
    public ActivityScenarioRule<MyClass> activityRule =
            new ActivityScenarioRule(MyClass.class);

    @Test
    public void myClassMethod_ReturnsTrue() { ... }
}

ServiceTestRule

This rule provides a simplified mechanism to start up and shut down your service before and after your test. It also guarantees that the service is successfully connected when starting a service or binding to one. The service can be started or bound using one of the helper methods. It automatically stops or unbinds after the test completes and any methods annotated with @After are finished.

Kotlin

@RunWith(AndroidJUnit4::class.java)
@MediumTest
class MyServiceTest {
    @get:Rule
    val serviceRule = ServiceTestRule()

    @Test fun testWithStartedService() {
        serviceRule.startService(
            Intent(ApplicationProvider.getApplicationContext<Context>(),
            MyService::class.java))

        // Add your test code here.
    }

    @Test fun testWithBoundService() {
        val binder = serviceRule.bindService(
            Intent(ApplicationProvider.getApplicationContext(),
            MyService::class.java))
        val service = (binder as MyService.LocalBinder).service
        assertThat(service.doSomethingToReturnTrue()).isTrue()
    }
}

Java

@RunWith(AndroidJUnit4.class)
@MediumTest
public class MyServiceTest {
    @Rule
    public final ServiceTestRule serviceRule = new ServiceTestRule();

    @Test
    public void testWithStartedService() {
        serviceRule.startService(
                new Intent(ApplicationProvider.getApplicationContext(),
                MyService.class));
        // Add your test code here.
    }

    @Test
    public void testWithBoundService() {
        IBinder binder = serviceRule.bindService(
                new Intent(ApplicationProvider.getApplicationContext(),
                MyService.class));
        MyService service = ((MyService.LocalBinder) binder).getService();
        assertThat(service.doSomethingToReturnTrue()).isTrue();
    }
}

Additional resources

For more information about using JUnit rules in Android tests, consult the following resources.

Samples