সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
AndroidX পরীক্ষায় AndroidJUnitRunner এর সাথে ব্যবহার করা JUnit নিয়মের একটি সেট অন্তর্ভুক্ত রয়েছে। JUnit নিয়মগুলি আরও নমনীয়তা প্রদান করে এবং পরীক্ষায় প্রয়োজনীয় বয়লারপ্লেট কোড হ্রাস করে। উদাহরণস্বরূপ, তারা একটি নির্দিষ্ট কার্যকলাপ শুরু করতে ব্যবহার করা যেতে পারে।
কার্যকলাপের দৃশ্যের নিয়ম
এই নিয়মটি একটি একক কার্যকলাপের কার্যকরী পরীক্ষা প্রদান করে। নিয়মটি @Test এর সাথে টীকা করা প্রতিটি পরীক্ষার আগে, সেইসাথে @Before এর সাথে টীকা করা যেকোনো পদ্ধতির আগে নির্বাচিত কার্যকলাপ চালু করে। নিয়মটি পরীক্ষা শেষ হওয়ার পরে কার্যকলাপ বন্ধ করে দেয় এবং সমস্ত পদ্ধতি @After ফিনিশ দিয়ে টীকা করা হয়। আপনার পরীক্ষার যুক্তিতে প্রদত্ত ক্রিয়াকলাপ অ্যাক্সেস করতে, ActivityScenarioRule.getScenario().onActivity() তে চালানোযোগ্য একটি কলব্যাক প্রদান করুন।
নিম্নলিখিত কোড স্নিপেট দেখায় কিভাবে আপনার পরীক্ষার যুক্তিতে ActivityScenarioRule অন্তর্ভুক্ত করতে হয়:
কোটলিন
@RunWith(AndroidJUnit4::class.java)@LargeTestclassMyClassTest{@get:RulevalactivityRule=ActivityScenarioRule(MyClass::class.java)@TestfunmyClassMethod_ReturnsTrue(){activityRule.scenario.onActivity{…}// Optionally, access the activity.}}
এই নিয়মটি পরীক্ষার আগে আপনার পরিষেবা চালু করার এবং আগে এবং পরে এটি বন্ধ করার জন্য একটি সরলীকৃত প্রক্রিয়া প্রদান করে। আপনি সাহায্যকারী পদ্ধতিগুলির একটি দিয়ে পরিষেবাটি শুরু বা আবদ্ধ করতে পারেন। পরীক্ষা শেষ হওয়ার পরে এটি স্বয়ংক্রিয়ভাবে বন্ধ হয়ে যায় বা বন্ধ হয়ে যায় এবং @After দিয়ে টীকা করা যেকোন পদ্ধতি শেষ হয়ে যায়।
কোটলিন
@RunWith(AndroidJUnit4::class.java)@MediumTestclassMyServiceTest{@get:RulevalserviceRule=ServiceTestRule()@TestfuntestWithStartedService(){serviceRule.startService(Intent(ApplicationProvider.getApplicationContext<Context>(),MyService::class.java))// Add your test code here.}@TestfuntestWithBoundService(){valbinder=serviceRule.bindService(Intent(ApplicationProvider.getApplicationContext(),MyService::class.java))valservice=(binderasMyService.LocalBinder).serviceassertThat(service.doSomethingToReturnTrue()).isTrue()}}
জাভা
@RunWith(AndroidJUnit4.class)@MediumTestpublicclassMyServiceTest{@RulepublicfinalServiceTestRuleserviceRule=newServiceTestRule();@TestpublicvoidtestWithStartedService(){serviceRule.startService(newIntent(ApplicationProvider.getApplicationContext(),MyService.class));// Add your test code here.}@TestpublicvoidtestWithBoundService(){IBinderbinder=serviceRule.bindService(newIntent(ApplicationProvider.getApplicationContext(),MyService.class));MyServiceservice=((MyService.LocalBinder)binder).getService();assertThat(service.doSomethingToReturnTrue()).isTrue();}}
অতিরিক্ত সম্পদ
অ্যান্ড্রয়েড পরীক্ষায় JUnit নিয়মগুলি ব্যবহার করার বিষয়ে আরও তথ্যের জন্য, নিম্নলিখিত সংস্থানগুলি দেখুন৷
মৌলিক নমুনা : ActivityScenarioRule এর সহজ ব্যবহার।
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি 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,["# JUnit4 rules with AndroidX Test\n\nAndroidX Test includes a set of [JUnit rules](https://github.com/junit-team/junit4/wiki/Rules) to be used with the\n[AndroidJUnitRunner](/training/testing/junit-runner). JUnit rules provide more flexibility and reduce the\nboilerplate code required in tests. For example, they can be used to start a\nspecific activity.\n\nActivityScenarioRule\n--------------------\n\nThis rule provides functional testing of a single activity. The rule launches\nthe chosen activity before each test annotated with `@Test`, as well as before\nany method annotated with `@Before`. The rule terminates the activity after the\ntest completes and all methods annotated with `@After` finish. To access the given\nactivity in your test logic, provide a callback runnable to\n`ActivityScenarioRule.getScenario().onActivity()`.\n\nThe following code snippet demonstrates how to incorporate\n`ActivityScenarioRule` into your testing logic: \n\n### Kotlin\n\n```kotlin\n@RunWith(AndroidJUnit4::class.java)\n@LargeTest\nclass MyClassTest {\n @get:Rule\n val activityRule = ActivityScenarioRule(MyClass::class.java)\n\n @Test fun myClassMethod_ReturnsTrue() {\n activityRule.scenario.onActivity { ... } // Optionally, access the activity.\n }\n}\n```\n\n### Java\n\n```java\npublic class MyClassTest {\n @Rule\n public ActivityScenarioRule<MyClass> activityRule =\n new ActivityScenarioRule(MyClass.class);\n\n @Test\n public void myClassMethod_ReturnsTrue() { ... }\n}\n```\n| **Note:** in order to test fragments in isolation, you can use the `FragmentScenario` class from the [AndroidX fragment-testing library](/guide/fragments/test).\n\nServiceTestRule\n---------------\n\nThis rule provides a simplified mechanism to launch your service before the\ntests and shut it down before and after. You can start or bind the service with\none of the helper methods. It automatically stops or unbinds after the test\ncompletes and any methods annotated with `@After` have finished.\n**Note:** This rule doesn't support `IntentService`. This is because the service is destroyed when `IntentService.onHandleIntent(Intent)` finishes all outstanding commands, so there is no guarantee to establish a successful connection in a timely manner. \n\n### Kotlin\n\n```kotlin\n@RunWith(AndroidJUnit4::class.java)\n@MediumTest\nclass MyServiceTest {\n @get:Rule\n val serviceRule = ServiceTestRule()\n\n @Test fun testWithStartedService() {\n serviceRule.startService(\n Intent(ApplicationProvider.getApplicationContext\u003cContext\u003e(),\n MyService::class.java))\n // Add your test code here.\n }\n\n @Test fun testWithBoundService() {\n val binder = serviceRule.bindService(\n Intent(ApplicationProvider.getApplicationContext(),\n MyService::class.java))\n val service = (binder as MyService.LocalBinder).service\n assertThat(service.doSomethingToReturnTrue()).isTrue()\n }\n}\n```\n\n### Java\n\n```java\n@RunWith(AndroidJUnit4.class)\n@MediumTest\npublic class MyServiceTest {\n @Rule\n public final ServiceTestRule serviceRule = new ServiceTestRule();\n\n @Test\n public void testWithStartedService() {\n serviceRule.startService(\n new Intent(ApplicationProvider.getApplicationContext(),\n MyService.class));\n // Add your test code here.\n }\n\n @Test\n public void testWithBoundService() {\n IBinder binder = serviceRule.bindService(\n new Intent(ApplicationProvider.getApplicationContext(),\n MyService.class));\n MyService service = ((MyService.LocalBinder) binder).getService();\n assertThat(service.doSomethingToReturnTrue()).isTrue();\n }\n}\n```\n\nAdditional resources\n--------------------\n\nFor more information about using JUnit rules in Android tests, consult the\nfollowing resources.\n\n### Documentation\n\n- [Test your fragments](/guide/fragments/test) guide, to test fragments in isolation.\n- [Testing your Compose layout](/jetpack/compose/testing), to test UIs made with Compose.\n\n### Samples\n\n- [BasicSample](https://github.com/android/testing-samples/tree/main/ui/espresso/BasicSample): Simple usage of `ActivityScenarioRule`."]]