Espresso-Intents 是 Espresso 的擴充功能,可驗證及擷取受測應用程式傳送的意圖。這就像是 Android Intent 專用的 Mockito。
如果應用程式會將功能委派給其他應用程式或平台,您可以使用 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
類別的擴充功能,可讓您在功能性 UI 測試中輕鬆使用 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 比對器定義。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")); }
Stubbing
使用類似 Mockito.when()
的 intending()
方法,您可以為透過 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: 模擬使用者透過相機擷取點陣圖。