Langsung ke konten

Paling sering dikunjungi

Terakhir dikunjungi

navigation

Testing Your Service

Dependencies and Prerequisites

This lesson teaches you to

  1. Set Up Your Testing Environment
  2. Create an Integrated Test for Services
  3. Run Integration Tests for Services

You should also read

Try it out

If you are implementing a local Service as a component of your app, you should test the Service to ensure that it doesn't behave in an unexpected way. You can create instrumented unit tests to verify that the behavior in the Service is correct; for example, the service stores and returns valid data values and performs data operations correctly.

The Android Testing Support Library provides an API for testing your Service objects in isolation. The ServiceTestRule class is a JUnit 4 rule that starts your service before your unit test methods run, and shuts down the service after tests complete. By using this test rule, you ensure that the connection to the service is always established before your test method runs. To learn more about JUnit 4 rules, see the JUnit documentation.

Note: The ServiceTestRule class does not support testing of IntentService objects. If you need to test a IntentService object, you should encapsulate the logic in a separate class and create a corresponding unit test instead.

Set Up Your Testing Environment

Before building your integration test for the service, make sure to configure your project for instrumented tests, as described in Getting Started with Testing.

Create an Integration Test for Services

Your integration test should be written as a JUnit 4 test class. To learn more about creating JUnit 4 test classes and using JUnit 4 assertion methods, see Create an Instrumented Unit Test Class.

To create an integration test for your service, add the @RunWith(AndroidJUnit4.class) annotation at the beginning of your test class definition. You also need to specify the AndroidJUnitRunner class that the Android Testing Support Library provides as your default test runner. This step is described in more detail in Run Instrumented Unit Tests.

Next, create a ServiceTestRule instance in your test by using the @Rule annotation.

@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();

The following example shows how you might implement an integration test for a service. The test method testWithBoundService verifies that the app binds successfully to a local service and that the service interface behaves correctly.

@Test
public void testWithBoundService() throws TimeoutException {
    // Create the service Intent.
    Intent serviceIntent =
            new Intent(InstrumentationRegistry.getTargetContext(),
                LocalService.class);

    // Data can be passed to the service via the Intent.
    serviceIntent.putExtra(LocalService.SEED_KEY, 42L);

    // Bind the service and grab a reference to the binder.
    IBinder binder = mServiceRule.bindService(serviceIntent);

    // Get the reference to the service, or you can call
    // public methods on the binder directly.
    LocalService service =
            ((LocalService.LocalBinder) binder).getService();

    // Verify that the service is working correctly.
    assertThat(service.getRandomInt(), is(any(Integer.class)));
}

Run Integration Tests for Services

You can run integration tests from Android Studio or from the command-line. Make sure to specify AndroidJUnitRunner as the default instrumentation runner in your project.

To run the integration test for your service, follow the steps for running instrumented tests described in Getting Started with Testing.

Situs ini menggunakan cookies untuk menyimpan preferensi Anda tentang bahasa di situs tertentu dan opsi tampilan.

Dapatkan berita dan tips terbaru dari developer Android yang akan membantu Anda mencapai kesuksesan di Google Play.

* Wajib Diisi

Hore!

Ikuti Google Developers di WeChat

Buka situs ini dalam bahasa ?

Anda meminta halaman dalam bahasa , namun preferensi bahasa untuk situs ini adalah .

Ingin mengubah preferensi bahasa dan mengakses situs ini dalam bahasa ? Jika nanti Anda ingin mengubah preferensi bahasa, gunakan menu bahasa di bagian bawah setiap halaman.

Kelas ini memerlukan tingkat API atau yang lebih tinggi

Dokumen ini tersembunyi karena level API yang Anda pilih untuk dokumentasi ini adalah . Anda dapat mengubah level API dokumentasi dengan pemilih di atas menu navigasi sebelah kiri.

Untuk informasi lebih lanjut tentang menentukan level API yang diperlukan aplikasi Anda, baca Mendukung Versi Platform Yang Berbeda.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)