آزمایش کردن سرویس

اگر درحال پیاده‌سازی Service محلی به‌عنوان مؤلفه‌ای از برنامه‌تان هستید، می‌توانید آزمایش‌های ابزاردار ایجاد کنید تا تأیید کنید که رفتار آن درست است.

AndroidX Test میانای برنامه‌سازی کاربردی‌ای برای آزمایش کردن اشیای Service به‌صورت مجزا ارائه می‌دهد. کلاس ServiceTestRule یک قانون JUnit 4 است که سرویس شما را قبل‌از اجرای روش‌های آزمایش واحد شما راه‌اندازی می‌کند و پس‌از تکمیل آزمایش‌ها، سرویس را خاموش می‌کند. برای کسب اطلاعات بیشتر درباره قوانین JUnit 4، به اسناد JUnit مراجعه کنید.

راه‌اندازی محیط آزمایشی

قبل‌از ساختن آزمایش یکپارچه‌سازی برای سرویس، حتماً پروژه‌تان را برای آزمایش‌های ابزاردار پیکربندی کنید، همان‌طور که در راه‌اندازی پروژه برای AndroidX Test شرح داده شده است.

ایجاد کردن آزمون یکپارچه‌سازی برای سرویس‌ها

آزمون یکپارچه‌سازی شما باید به‌عنوان کلاس آزمون JUnit 4 نوشته شود. برای کسب اطلاعات بیشتر درباره ایجاد کلاس‌های آزمایشی JUnit 4 و استفاده از روش‌های ادعای JUnit 4، به ایجاد کلاس آزمایشی ابزاردار مراجعه کنید.

بااستفاده از حاشیه‌نویسی @Rule، نمونه ServiceTestRule را در آزمون خود ایجاد کنید.

کاتلین

@get:Rule
val serviceRule = ServiceTestRule()

جاوا

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

مثال زیر نشان می‌دهد که چگونه می‌توانید یک آزمایش ادغام را برای یک سرویس پیاده‌سازی کنید. روش آزمایشی testWithBoundService() درستی‌سنجی می‌کند که برنامه با موفقیت به سرویس محلی متصل می‌شود و میانای سرویس به‌درستی رفتار می‌کند.

کاتلین

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

جاوا

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

منابع بیشتر

برای کسب اطلاعات بیشتر درباره این موضوع، به منابع تکمیلی زیر مراجعه کنید.

نمونه‌ها