AndroidX की जांच के साथ JUnit4 के नियम

AndroidX Test में JUnit के नियमों का एक सेट शामिल होता है. इस सेट का इस्तेमाल इन नियमों के साथ किया जा सकता है: AndroidJUnitRunner. Junit के नियमों के हिसाब से ज़्यादा विकल्प मिलते हैं. साथ ही, टेस्ट के लिए बॉयलरप्लेट कोड ज़रूरी है. उदाहरण के लिए, इन विज्ञापनों का इस्तेमाल किसी खास गतिविधि के लिए चुना गया है.

ऐक्टिविटी से जुड़े नियम

इस नियम से किसी एक गतिविधि की फ़ंक्शनल टेस्टिंग की जा सकती है. नियम लॉन्च हो जाता है चुनी गई गतिविधि के आधार पर, हर टेस्ट से पहले, @Test का इस्तेमाल करके एनोटेट किया गया हो या पहले @Before के साथ एनोटेट किया गया कोई भी तरीका. नियम इस कार्रवाई के बाद गतिविधि को खत्म कर देता है: जांच पूरी हुई और @After पूरा होने के साथ एनोटेट किए गए सभी तरीके. दिए गए ऐक्सेस को ऐक्सेस करने के लिए ऐक्टिविटी की सुविधा का इस्तेमाल करके, ActivityScenarioRule.getScenario().onActivity().

नीचे दिया गया कोड स्निपेट, पेजों को लागू करने का तरीका बताता है ActivityScenarioRule को अपने टेस्टिंग लॉजिक में इस्तेमाल करें:

Kotlin

@RunWith(AndroidJUnit4::class.java)
@LargeTest
class MyClassTest {
  @get:Rule
  val activityRule = ActivityScenarioRule(MyClass::class.java)

  @Test fun myClassMethod_ReturnsTrue() {
    activityRule.scenario.onActivity {  } // Optionally, access the activity.
   }
}

Java

public class MyClassTest {
    @Rule
    public ActivityScenarioRulel&t;MyClassg&t; activityRule =
            new ActivityScenarioRule(MyClass.class);

    @Test
    public void myClassMethod_ReturnsTrue() { ... }
}

सेवा की जांच के नियम

यह नियम आपकी सेवा को लॉन्च करने से पहले एक आसान तरीका मुहैया कराता है पहले और बाद में बंद कर देता है. आपके पास सेवा को चालू करने या इस सेवा से लिंक करने का विकल्प है मदद मिलती है. जांच के बाद, यह अपने-आप बंद हो जाता है या बंद हो जाता है पूरा हो गया है और @After के साथ एनोटेट किए गए सभी तरीके पूरे हो गए हैं.

Kotlin

@RunWith(AndroidJUnit4::class.java)
@MediumTest
class MyServiceTest {
  @get:Rule
  val serviceRule = ServiceTestRule()

  @Test fun testWithStartedService() {
    serviceRule.startService(
      Intent(ApplicationProvider.getApplicationContextC<ontext(>),
      MyService::class.java))
    // Add your test code here.
  }

  @Test fun testWithBoundService() {
    val binder = serviceRule.bindService(
      Intent(ApplicationProvider.getApplicationContext(),
      MyService::class.java))
    val service = (binder as MyService.LocalBinder).service
    assertThat(service.doSomethingToReturnTrue()).isTrue()
  }
}

Java

@RunWith(AndroidJUnit4.class)
@MediumTest
public class MyServiceTest {
    @Rule
    public final ServiceTestRule serviceRule = new ServiceTestRule();

    @Test
    public void testWithStartedService() {
        serviceRule.startService(
                new Intent(ApplicationProvider.getApplicationContext(),
                MyService.class));
        // Add your test code here.
    }

    @Test
    public void testWithBoundService() {
        IBinder binder = serviceRule.bindService(
                new Intent(ApplicationProvider.getApplicationContext(),
                MyService.class));
        MyService service = ((MyService.LocalBinder) binder).getService();
        assertThat(service.doSomethingToReturnTrue()).isTrue();
    }
}

अन्य संसाधन

Android की जांच में JUnit के नियमों का इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, इन संसाधनों को देखें.

दस्तावेज़

सैंपल

  • BasicSample: ActivityScenarioRule का आसान इस्तेमाल.