Espresso-Intents, Espresso का एक एक्सटेंशन है. यह टेस्ट किए जा रहे ऐप्लिकेशन से भेजे गए इंटेंट की पुष्टि करने और स्टब करने की सुविधा देता है. यह Mockito की तरह है, लेकिन Android Intent के लिए.
अगर आपका ऐप्लिकेशन, फ़ंक्शन को अन्य ऐप्लिकेशन या प्लैटफ़ॉर्म को सौंपता है, तो Espresso-Intents का इस्तेमाल किया जा सकता है. इससे, अपने ऐप्लिकेशन के लॉजिक पर फ़ोकस किया जा सकता है. साथ ही, यह माना जा सकता है कि अन्य ऐप्लिकेशन या प्लैटफ़ॉर्म ठीक से काम करेंगे. Espresso-Intents की मदद से, अपने भेजे गए इंटेंट को मैच और पुष्टि की जा सकती है. इसके अलावा, इंटेंट के असली जवाबों के बजाय स्टब जवाब भी दिए जा सकते हैं.
अपने प्रोजेक्ट में Espresso-Intents को शामिल करना
अपने ऐप्लिकेशन की app/build.gradle
फ़ाइल में, dependencies
के अंदर यह लाइन जोड़ें:
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-intents:3.6.1')
Espresso-Intents, सिर्फ़ Espresso 2.1+ और Android टेस्टिंग लाइब्रेरी के 0.3+ वर्शन के साथ काम करता है. इसलिए, पक्का करें कि आपने इन लाइनों को भी अपडेट कर दिया हो:
Groovy
androidTestImplementation 'androidx.test:runner:1.6.1' androidTestImplementation 'androidx.test:rules:1.6.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
Kotlin
androidTestImplementation('androidx.test:runner:1.6.1') androidTestImplementation('androidx.test:rules:1.6.1') androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
जांच के नियम लिखना
Espresso-Intents टेस्ट लिखने से पहले, IntentsTestRule
सेट अप करें. यह क्लास ActivityTestRule
का एक्सटेंशन है. इससे फ़ंक्शनल यूआई टेस्ट में Espresso-Intents API का इस्तेमाल करना आसान हो जाता है. IntentsTestRule
, @Test
से एनोटेट किए गए हर टेस्ट से पहले Espresso-Intents को शुरू करता है और हर टेस्ट रन के बाद Espresso-Intents को बंद करता है.
यहां दिया गया कोड स्निपेट, IntentsTestRule
का एक उदाहरण है:
Kotlin
@get:Rule val intentsTestRule = IntentsTestRule(MyActivity::class.java)
Java
@Rule public IntentsTestRule<MyActivity> intentsTestRule = new IntentsTestRule<>(MyActivity.class);
मिलता-जुलता
Espresso-Intents, कुछ मैचिंग शर्तों के आधार पर आउटगोइंग इंटेंट को इंटरसेप्ट करने की सुविधा देता है. इन शर्तों को Hamcrest Matchers का इस्तेमाल करके तय किया जाता है. Hamcrest की मदद से ये काम किए जा सकते हैं:
- मौजूदा इंटेंट मैच करने वाले टूल का इस्तेमाल करना: यह सबसे आसान विकल्प है. इसलिए, इसे हमेशा प्राथमिकता दी जानी चाहिए.
- अपने हिसाब से इंटेंट मैच करने की सुविधा लागू करें: यह सबसे ज़्यादा फ़ायदेमंद विकल्प है. ज़्यादा जानकारी के लिए, Hamcrest ट्यूटोरियल में "कस्टम मैचर लिखना" सेक्शन देखें.
Espresso-Intents, इंटेंट की पुष्टि करने के लिए intended()
और स्टबिंग के लिए intending()
तरीके उपलब्ध कराता है. दोनों, Hamcrest Matcher<Intent>
ऑब्जेक्ट को आर्ग्युमेंट के तौर पर लेते हैं.
यहां दिए गए कोड स्निपेट में, इंटेंट की पुष्टि करने का तरीका दिखाया गया है. इसमें मौजूदा इंटेंट मैच करने वालों का इस्तेमाल किया जाता है. ये मैच करने वाले, ब्राउज़र शुरू करने वाले आउटगोइंग इंटेंट से मैच करते हैं:
Kotlin
assertThat(intent).hasAction(Intent.ACTION_VIEW) assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE) assertThat(intent).hasData(Uri.parse("www.google.com")) assertThat(intent).extras().containsKey("key1") assertThat(intent).extras().string("key1").isEqualTo("value1") assertThat(intent).extras().containsKey("key2") assertThat(intent).extras().string("key2").isEqualTo("value2")
Java
assertThat(intent).hasAction(Intent.ACTION_VIEW); assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE); assertThat(intent).hasData(Uri.parse("www.google.com")); assertThat(intent).extras().containsKey("key1"); assertThat(intent).extras().string("key1").isEqualTo("value1"); assertThat(intent).extras().containsKey("key2"); assertThat(intent).extras().string("key2").isEqualTo("value2");
इंटेंट की पुष्टि करना
Espresso-Intents, उन सभी इंटेंट को रिकॉर्ड करता है जो टेस्ट किए जा रहे ऐप्लिकेशन से गतिविधियां लॉन्च करने की कोशिश करते हैं. intended()
तरीके का इस्तेमाल करके, यह दावा किया जा सकता है कि किसी दिए गए इंटेंट को देखा गया है. यह तरीका Mockito.verify()
के जैसा ही है. हालांकि, Espresso-Intents, इंटेंट के जवाबों को तब तक स्टब नहीं करता, जब तक कि आप इसे ऐसा करने के लिए साफ़ तौर पर कॉन्फ़िगर न करें.
नीचे दिया गया कोड स्निपेट, एक उदाहरण टेस्ट है. यह टेस्ट, बाहर जाने वाले ऐसे इंटेंट की पुष्टि करता है जो "फ़ोन" से जुड़ी बाहरी गतिविधि को लॉन्च करता है. हालांकि, यह इंटेंट के जवाबों को स्टब नहीं करता:
Kotlin
@Test fun validateIntentSentToPackage() { // User action that results in an external "phone" activity being launched. user.clickOnView(system.getView(R.id.callButton)) // Using a canned RecordedIntentMatcher to validate that an intent resolving // to the "phone" activity has been sent. intended(toPackage("com.android.phone")) }
Java
@Test public void validateIntentSentToPackage() { // User action that results in an external "phone" activity being launched. user.clickOnView(system.getView(R.id.callButton)); // Using a canned RecordedIntentMatcher to validate that an intent resolving // to the "phone" activity has been sent. intended(toPackage("com.android.phone")); }
स्टबिंग
intending()
तरीके का इस्तेमाल करके, Mockito.when()
की तरह ही, startActivityForResult()
की मदद से लॉन्च की गई गतिविधियों के लिए स्टब रिस्पॉन्स दिया जा सकता है. यह खास तौर पर बाहरी गतिविधियों के लिए फ़ायदेमंद है, क्योंकि न तो बाहरी गतिविधि के यूज़र इंटरफ़ेस में बदलाव किया जा सकता है और न ही टेस्ट की जा रही गतिविधि को वापस भेजे गए ActivityResult
को कंट्रोल किया जा सकता है.
यहां दिए गए कोड स्निपेट, activityResult_DisplaysContactsPhoneNumber()
टेस्ट को लागू करने का एक उदाहरण दिखाते हैं. इससे यह पुष्टि की जाती है कि जब कोई उपयोगकर्ता, टेस्ट किए जा रहे ऐप्लिकेशन में "संपर्क करें" गतिविधि लॉन्च करता है, तो संपर्क करने के लिए फ़ोन नंबर दिखता है:
किसी गतिविधि को लॉन्च करने पर, नतीजे को वापस लाने के लिए उसे बनाएं. इस उदाहरण में, "contacts" को भेजे गए सभी इंटेंट को टेस्ट किया जाता है. साथ ही,
RESULT_OK
नतीजे वाले कोड का इस्तेमाल करके, मान्यActivityResult
के साथ उनके जवाबों को स्टब किया जाता हैKotlin
val resultData = Intent() val phoneNumber = "123-345-6789" resultData.putExtra("phone", phoneNumber) val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
Java
Intent resultData = new Intent(); String phoneNumber = "123-345-6789"; resultData.putExtra("phone", phoneNumber); ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);
Espresso को "contacts" इंटेंट के सभी इनवोकेशन के जवाब में स्टब नतीजे का ऑब्जेक्ट देने का निर्देश दें:
Kotlin
intending(toPackage("com.android.contacts")).respondWith(result)
Java
intending(toPackage("com.android.contacts")).respondWith(result);
पुष्टि करें कि गतिविधि लॉन्च करने के लिए इस्तेमाल किए गए ऐक्शन से, स्टब का अनुमानित नतीजा मिलता है. इस मामले में, उदाहरण की जांच में यह देखा जाता है कि"संपर्क गतिविधि" लॉन्च होने पर, फ़ोन नंबर "123-345-6789" दिखता है या नहीं:
Kotlin
onView(withId(R.id.pickButton)).perform(click()) onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)))
Java
onView(withId(R.id.pickButton)).perform(click()); onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)));
यहां activityResult_DisplaysContactsPhoneNumber()
टेस्ट की पूरी जानकारी दी गई है:
Kotlin
@Test fun activityResult_DisplaysContactsPhoneNumber() { // Build the result to return when the activity is launched. val resultData = Intent() val phoneNumber = "123-345-6789" resultData.putExtra("phone", phoneNumber) val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData) // Set up result stubbing when an intent sent to "contacts" is seen. intending(toPackage("com.android.contacts")).respondWith(result) // User action that results in "contacts" activity being launched. // Launching activity expects phoneNumber to be returned and displayed. onView(withId(R.id.pickButton)).perform(click()) // Assert that the data we set up above is shown. onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber))) }
Java
@Test public void activityResult_DisplaysContactsPhoneNumber() { // Build the result to return when the activity is launched. Intent resultData = new Intent(); String phoneNumber = "123-345-6789"; resultData.putExtra("phone", phoneNumber); ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData); // Set up result stubbing when an intent sent to "contacts" is seen. intending(toPackage("com.android.contacts")).respondWith(result); // User action that results in "contacts" activity being launched. // Launching activity expects phoneNumber to be returned and displayed. onView(withId(R.id.pickButton)).perform(click()); // Assert that the data we set up above is shown. onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber))); }
अन्य संसाधन
Android टेस्ट में Espresso-Intents का इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, यहां दिए गए संसाधन देखें.
सैंपल
- IntentsBasicSample:
intended()
औरintending()
का बुनियादी इस्तेमाल. - IntentsAdvancedSample: यह ऐप्लिकेशन, कैमरा इस्तेमाल करके बिटमैप फ़ेच करने वाले उपयोगकर्ता की गतिविधि का सिम्युलेट करता है.