Kiểm thử dịch vụ

Nếu đang triển khai Service cục bộ làm thành phần của ứng dụng, thì bạn có thể tạo kiểm thử đo lường để xác minh rằng hành vi của kiểm thử là chính xác.

Kiểm thử AndroidX cung cấp một API để kiểm thử các đối tượng Service để tách biệt. Lớp ServiceTestRule là một quy tắc JUnit 4. Quy tắc này sẽ khởi động dịch vụ của bạn trước khi chạy phương thức kiểm thử đơn vị và tắt dịch vụ sau khi quá trình kiểm thử hoàn tất. Để tìm hiểu thêm về quy tắc JUnit 4, hãy xem tài liệu về JUnit.

Thiết lập môi trường kiểm thử

Trước khi tạo kiểm thử tích hợp cho dịch vụ, hãy nhớ định cấu hình dự án cho các kiểm thử đo lường, như mô tả trong bài viết Thiết lập dự án cho Kiểm thử AndroidX.

Tạo bài kiểm thử tích hợp cho các dịch vụ

Bạn nên viết chương trình kiểm thử tích hợp dưới dạng lớp kiểm thử JUnit 4. Để tìm hiểu thêm về cách tạo lớp kiểm thử JUnit 4 và sử dụng phương thức xác nhận JUnit 4, hãy xem phần Tạo lớp kiểm thử đo lường.

Tạo một thực thể ServiceTestRule trong kiểm thử bằng cách sử dụng chú giải @Rule.

Kotlin


@get:Rule
val serviceRule = ServiceTestRule()

Java


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

Ví dụ sau cho thấy cách bạn có thể triển khai bài kiểm thử tích hợp cho một dịch vụ. Phương thức kiểm thử testWithBoundService() xác minh rằng ứng dụng liên kết thành công với một dịch vụ cục bộ và giao diện dịch vụ hoạt động đúng cách.

Kotlin


@Test
@Throws(TimeoutException::class)
fun testWithBoundService() {
  // Create the service Intent.
  val serviceIntent = Intent(
      ApplicationProvider.getApplicationContext<Context>(),
      LocalService::class.java
  ).apply {
    // Data can be passed to the service via the Intent.
    putExtra(SEED_KEY, 42L)
  }

  // Bind the service and grab a reference to the binder.
  val binder: IBinder = serviceRule.bindService(serviceIntent)

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

  // Verify that the service is working correctly.
  assertThat(service.getRandomInt(), `is`(any(Int::class.java)))
}

Java


@Test
public void testWithBoundService() throws TimeoutException {
  // Create the service Intent.
  Intent serviceIntent =
      new Intent(ApplicationProvider.getApplicationContext(),
        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 = serviceRule.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()).isAssignableTo(Integer.class);
}

Tài nguyên khác

Để tìm hiểu thêm về chủ đề này, hãy tham khảo thêm các tài nguyên sau đây.

Mẫu