Espresso-Intents 是 Espresso 的擴充功能,可驗證及虛設待測應用程式傳送的意圖。這與 Mockito 類似,但適用於 Android 意圖。
如果應用程式將功能委派給其他應用程式或平台,您可以使用 Espresso-Intents 專注於自家應用程式的邏輯,同時假設其他應用程式或平台可正常運作。透過 Espresso-Intent,您可以比對並驗證傳出意圖,甚至提供虛設常式回應,以取代實際意圖回應。
在專案中加入 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 類別的擴充功能,可讓您在功能性 UI 測試中輕鬆使用 Espresso-Intents API。IntentsTestRule 會在每次加上 @Test 註解的測試前初始化 Espresso-Intents,並在每次測試執行後釋出 Espresso-Intents。
以下程式碼片段是 IntentsTestRule 的範例:
@get:Rule val intentsTestRule = IntentsTestRule(MyActivity::class.java)
比對
Espresso-Intents 可根據特定比對條件攔截傳出意圖,這些條件是使用 Hamcrest 比對器定義。Hamcrest 可讓您:
- 使用現有意圖比對器:最簡單的選項,幾乎一律是首選。
- 自行實作意圖比對器:這是最靈活的做法,詳情請參閱 Hamcrest 教學課程中「編寫自訂比對器」一節。
Espresso-Intents 提供 intended() 和 intending() 方法,分別用於意圖驗證和虛設常式。兩者都採用 Hamcrest Matcher<Intent> 物件做為引數。
下列程式碼片段顯示意圖驗證,使用現有意圖比對器比對啟動瀏覽器的傳出意圖:
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 不會虛設意圖的回應。
下列程式碼片段是測試範例,可驗證啟動外部「電話」活動的傳出意圖,但不會虛設常式回應:
@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")) }
Stubbing
使用 intending() 方法 (類似於 Mockito.when()),您可以為使用 startActivityForResult() 啟動的活動提供虛設常式回應。這對外部活動特別有用,因為您無法操控外部活動的使用者介面,也無法控制傳回受測活動的 ActivityResult。
下列程式碼片段會實作 activityResult_DisplaysContactsPhoneNumber() 測試範例,驗證當使用者在受測應用程式中啟動「聯絡人」活動時,系統會顯示聯絡人電話號碼:
建構啟動特定活動時要傳回的結果。範例測試會攔截傳送至「contacts」的所有意圖,並使用結果代碼
RESULT_OK,以有效的ActivityResult虛設其回應val resultData = Intent() val phoneNumber = "123-345-6789" resultData.putExtra("phone", phoneNumber) val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
指示 Espresso 提供虛設常式結果物件,以回應所有「聯絡人」意圖的叫用:
intending(toPackage("com.android.contacts")).respondWith(result)
確認用於啟動活動的動作會產生預期的虛設常式結果。在本例中,範例測試會檢查啟動「聯絡人活動」時,是否傳回並顯示電話號碼 「123-345-6789」:
onView(withId(R.id.pickButton)).perform(click()) onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)))
以下是完整的 activityResult_DisplaysContactsPhoneNumber() 測試:
@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))) }
其他資源
如要進一步瞭解如何在 Android 測試中使用 Espresso-Intents,請參閱下列資源。
範例
- IntentsBasicSample:
intended()和intending()的基本用法。 - IntentsAdvancedSample:模擬使用者使用相機擷取點陣圖。