Hizmetinizi test etme

Uygulamanızın bileşeni olarak yerel bir Service uyguluyorsanız davranışının doğru olduğunu doğrulamak için araçlı testler oluşturabilirsiniz.

AndroidX Test, Service nesnelerinizi ayrı ayrı test etmeniz için bir API sağlar. ServiceTestRule sınıfı, birim test yöntemleriniz çalıştırılmadan önce hizmetinizi başlatan ve testler tamamlandıktan sonra hizmeti kapatan bir JUnit 4 kuralıdır. JUnit 4 kuralları hakkında daha fazla bilgi edinmek için JUnit belgelerine bakın.

Test ortamınızı ayarlama

Hizmet için entegrasyon testinizi oluşturmadan önce, projenizi AndroidX Test için proje oluşturma bölümünde açıklandığı gibi, donanımlı testler için yapılandırdığınızdan emin olun.

Hizmetler için entegrasyon testi oluşturma

Entegrasyon testiniz bir JUnit 4 test sınıfı olarak yazılmalıdır. JUnit 4 test sınıfları oluşturma ve JUnit 4 onay yöntemlerini kullanma hakkında daha fazla bilgi edinmek için Araçlı test sınıfı oluşturma bölümüne bakın.

@Rule notasyonunu kullanarak testinizde bir ServiceTestRule örneği oluşturun.

Kotlin


@get:Rule
val serviceRule = ServiceTestRule()

Java


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

Aşağıdaki örnekte, bir hizmet için entegrasyon testini nasıl uygulayabileceğiniz gösterilmektedir. testWithBoundService() test yöntemi, uygulamanın yerel bir hizmete başarılı bir şekilde bağlandığını ve hizmet arayüzünün doğru şekilde çalıştığını doğrular.

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);
}

Ek kaynaklar

Bu konu hakkında daha fazla bilgi için aşağıdaki ek kaynaklara başvurun.

Sana Özel