আপনার পরিষেবা পরীক্ষা করুন

আপনি যদি নিজের অ্যাপের কম্পোনেন্ট হিসেবে স্থানীয় Service প্রয়োগ করেন, তাহলে সেটির আচরণ সঠিক কিনা তা যাচাই করতে ইনস্ট্রুমেন্টেড টেস্ট তৈরি করতে পারবেন।

AndroidX Test আপনার Service অবজেক্ট আলাদাভাবে পরীক্ষা করার জন্য API প্রদান করে। ServiceTestRule ক্লাস হল JUnit 4 নিয়ম যা আপনার ইউনিট টেস্ট পদ্ধতি রান করার আগে আপনার পরিষেবা চালু করে এবং টেস্ট সম্পূর্ণ হওয়ার পরে পরিষেবা বন্ধ করে দেয়। JUnit 4 নিয়ম সম্পর্কে আরও জানতে, JUnit ডকুমেন্টেশন দেখুন।

আপনার টেস্টিং এনভায়রনমেন্ট সেট-আপ করা

পরিষেবার জন্য ইন্টিগ্রেশন টেস্ট তৈরি করার আগে, AndroidX টেস্টের জন্য প্রোজেক্ট সেট-আপ করুন লিঙ্কে যেভাবে বর্ণনা করা হয়েছে সেই অনুযায়ী ইনস্ট্রুমেন্টেড টেস্টের জন্য আপনার প্রোজেক্ট কনফিগার করতে ভুলবেন না।

পরিষেবার জন্য ইন্টিগ্রেশন টেস্ট তৈরি করুন

আপনার ইন্টিগ্রেশন টেস্ট JUnit 4 টেস্ট ক্লাস হিসেবে লেখা উচিত। JUnit 4 টেস্ট ক্লাস তৈরি করা এবং JUnit 4 অ্যাসারশন পদ্ধতি ব্যবহার করার বিষয়ে আরও জানতে, ইনস্ট্রুমেন্টেড টেস্ট ক্লাস তৈরি করুন দেখুন।

@Rule অ্যানোটেশন ব্যবহার করে আপনার টেস্টে একটি ServiceTestRule ইনস্ট্যান্স তৈরি করুন।

Kotlin

@get:Rule
val serviceRule = ServiceTestRule()

জাভা

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

নিম্নলিখিত উদাহরণে দেখানো হয়েছে যে আপনি কীভাবে কোনও পরিষেবার জন্য ইন্টিগ্রেশন টেস্ট প্রয়োগ করতে পারেন। testWithBoundService() টেস্ট পদ্ধতি যাচাই করে যে অ্যাপটি কোনও লোকাল পরিষেবার সাথে সফলভাবে বাইন্ড করে এবং পরিষেবা ইন্টারফেস সঠিকভাবে কাজ করে।

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

জাভা

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

অতিরিক্ত রিসোর্স

এই বিষয়ে আরও জানতে, নিম্নলিখিত অতিরিক্ত রিসোর্স দেখুন।

নমুনা