সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
আপনি যদি আপনার অ্যাপের একটি উপাদান হিসাবে একটি স্থানীয় Service বাস্তবায়ন করেন, তাহলে আপনি তার আচরণ সঠিক কিনা তা যাচাই করার জন্য যন্ত্রযুক্ত পরীক্ষা তৈরি করতে পারেন।
অ্যান্ড্রয়েডএক্স টেস্ট আপনার Service বস্তুগুলিকে বিচ্ছিন্নভাবে পরীক্ষা করার জন্য একটি API প্রদান করে। ServiceTestRule ক্লাস হল একটি JUnit 4 নিয়ম যা আপনার ইউনিট পরীক্ষা পদ্ধতিগুলি চালানোর আগে আপনার পরিষেবা শুরু করে এবং পরীক্ষাগুলি সম্পূর্ণ হওয়ার পরে পরিষেবাটি বন্ধ করে দেয়। JUnit 4 নিয়ম সম্পর্কে আরও জানতে, JUnit ডকুমেন্টেশন দেখুন।
আপনার পরীক্ষার পরিবেশ সেট আপ করুন
পরিষেবার জন্য আপনার ইন্টিগ্রেশন পরীক্ষা তৈরি করার আগে, AndroidX টেস্টের জন্য সেট আপ প্রকল্পে বর্ণিত যন্ত্রযুক্ত পরীক্ষার জন্য আপনার প্রকল্পটি কনফিগার করা নিশ্চিত করুন।
পরিষেবাগুলির জন্য একটি ইন্টিগ্রেশন পরীক্ষা তৈরি করুন৷
আপনার ইন্টিগ্রেশন পরীক্ষা একটি JUnit 4 পরীক্ষার ক্লাস হিসাবে লেখা উচিত। JUnit 4 পরীক্ষার ক্লাস তৈরি করা এবং JUnit 4 দাবী পদ্ধতি ব্যবহার করার বিষয়ে আরও জানতে, একটি যন্ত্রযুক্ত পরীক্ষার ক্লাস তৈরি করুন দেখুন।
@Rule টীকা ব্যবহার করে আপনার পরীক্ষায় একটি ServiceTestRule উদাহরণ তৈরি করুন।
নিম্নলিখিত উদাহরণটি দেখায় যে আপনি কীভাবে একটি পরিষেবার জন্য একটি ইন্টিগ্রেশন পরীক্ষা বাস্তবায়ন করতে পারেন। পরীক্ষা পদ্ধতি testWithBoundService() যাচাই করে যে অ্যাপটি একটি স্থানীয় পরিষেবার সাথে সফলভাবে আবদ্ধ হয়েছে এবং পরিষেবা ইন্টারফেসটি সঠিকভাবে আচরণ করছে।
কোটলিন
@Test@Throws(TimeoutException::class)funtestWithBoundService(){// Create the service Intent.valserviceIntent=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.valbinder:IBinder=serviceRule.bindService(serviceIntent)// Get the reference to the service, or you can call// public methods on the binder directly.valservice:LocalService=(binderasLocalService.LocalBinder).getService()// Verify that the service is working correctly.assertThat(service.getRandomInt(),`is`(any(Int::class.java)))}
জাভা
@TestpublicvoidtestWithBoundService()throwsTimeoutException{// Create the service Intent.IntentserviceIntent=newIntent(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.IBinderbinder=serviceRule.bindService(serviceIntent);// Get the reference to the service, or you can call// public methods on the binder directly.LocalServiceservice=((LocalService.LocalBinder)binder).getService();// Verify that the service is working correctly.assertThat(service.getRandomInt()).isAssignableTo(Integer.class);}
অতিরিক্ত সম্পদ
এই বিষয় সম্পর্কে আরও জানতে, নিম্নলিখিত অতিরিক্ত সংস্থানগুলির সাথে পরামর্শ করুন৷
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# Test your service\n\nIf you are implementing a local [`Service`](/reference/android/app/Service) as a component of your app, you\ncan create [instrumented tests](/training/testing/unit-testing/instrumented-unit-tests) to verify that its behavior is correct.\n\n[AndroidX Test](/training/testing) provides an API for testing your `Service` objects in\nisolation. The [`ServiceTestRule`](/reference/androidx/test/rule/ServiceTestRule) class is a JUnit 4 rule that starts your\nservice before your unit test methods run, and shuts down the service after\ntests complete. To learn more about JUnit 4 rules, see the [JUnit\ndocumentation](https://github.com/junit-team/junit/wiki/Rules).\n| **Note:** The `ServiceTestRule` class does not support testing of [`IntentService`](/reference/android/app/IntentService) objects. If you need to test an `IntentService` object, you should encapsulate the logic in a separate class and create a corresponding unit test instead.\n\nSet up your testing environment\n-------------------------------\n\nBefore building your integration test for the service, make sure to configure\nyour project for instrumented tests, as described in [Set up project for\nAndroidX Test](/training/testing/set-up-project).\n\nCreate an integration test for services\n---------------------------------------\n\nYour integration test should be written as a JUnit 4 test class. To learn more\nabout creating JUnit 4 test classes and using JUnit 4 assertion methods, see\n[Create an instrumented test class](/training/testing/unit-testing/instrumented-unit-tests#create-instrumented).\n\nCreate a `ServiceTestRule` instance in your test by using the `@Rule`\nannotation. \n\n### Kotlin\n\n```kotlin\n@get:Rule\nval serviceRule = ServiceTestRule()\n```\n\n### Java\n\n```java\n@Rule\npublic final ServiceTestRule serviceRule = new ServiceTestRule();\n```\n\nThe following example shows how you might implement an integration test for a\nservice. The test method `testWithBoundService()` verifies that the app binds\nsuccessfully to a local service and that the service interface behaves\ncorrectly. \n\n### Kotlin\n\n```kotlin\n@Test\n@Throws(TimeoutException::class)\nfun testWithBoundService() {\n // Create the service Intent.\n val serviceIntent = Intent(\n ApplicationProvider.getApplicationContext\u003cContext\u003e(),\n LocalService::class.java\n ).apply {\n // Data can be passed to the service via the Intent.\n putExtra(SEED_KEY, 42L)\n }\n\n // Bind the service and grab a reference to the binder.\n val binder: IBinder = serviceRule.bindService(serviceIntent)\n\n // Get the reference to the service, or you can call\n // public methods on the binder directly.\n val service: LocalService = (binder as LocalService.LocalBinder).getService()\n\n // Verify that the service is working correctly.\n assertThat(service.getRandomInt(), `is`(any(Int::class.java)))\n}\n```\n\n### Java\n\n```java\n@Test\npublic void testWithBoundService() throws TimeoutException {\n // Create the service Intent.\n Intent serviceIntent =\n new Intent(ApplicationProvider.getApplicationContext(),\n LocalService.class);\n\n // Data can be passed to the service via the Intent.\n serviceIntent.putExtra(LocalService.SEED_KEY, 42L);\n\n // Bind the service and grab a reference to the binder.\n IBinder binder = serviceRule.bindService(serviceIntent);\n\n // Get the reference to the service, or you can call\n // public methods on the binder directly.\n LocalService service =\n ((LocalService.LocalBinder) binder).getService();\n\n // Verify that the service is working correctly.\n assertThat(service.getRandomInt()).isAssignableTo(Integer.class);\n}\n```\n\nAdditional resources\n--------------------\n\nTo learn more about this topic, consult the following additional resources.\n\n### Samples\n\n- [Service Test Code Samples](https://github.com/android/testing-samples/tree/main/integration/ServiceTestRuleSample)"]]