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.

ActivityTestRule

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, call ActivityTestRule.getActivity().

Note: AndroidX Test includes another API, ActivityScenario, that is currently in beta. This API works in a variety of testing environments and provides thread safety within the tests that use it.

We encourage you to try out this API to see how it can help you drive the lifecycles of your app's activities.

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

Kotlin

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

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

Java

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyClassTest {
    @Rule
    public ActivityTestRule<MyClass> mActivityRule =
            new ActivityTestRule(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 mServiceRule = new ServiceTestRule();

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

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