我正在对Google教程之一的espresso测试进行审查,以学习espresso测试。我正在尝试运行测试,但失败了,并且不确定问题出在哪里。我正在这里进行一些研究,以查找错误并正确运行测试,但我不知道问题出在哪里。
@RunWith(AndroidJUnit4::class)
class PlantDetailFragmentTest {
@Rule
@JvmField
val activityTestRule = ActivityTestRule(GardenActivity::class.java)
@Before
fun jumpToPlantDetailFragment() {
activityTestRule.activity.apply {
runOnUiThread {
val bundle = Bundle().apply { putString("plantId", testPlant.plantId) }
findNavController(R.id.garden_nav_fragment).navigate(R.id.plant_detail_fragment, bundle)
}
}
}
@Test
fun testShareTextIntent() {
val shareText = activityTestRule.activity.getString(R.string.share_text_plant, testPlant.name)
Intents.init()
onView(withId(R.id.action_share)).perform(click())
intended(
chooser(
allOf(
hasAction(Intent.ACTION_SEND),
hasType("text/plain"),
hasExtra(Intent.EXTRA_TEXT, shareText)
)
)
)
Intents.release()
// dismiss the Share Dialog
InstrumentationRegistry.getInstrumentation()
.uiAutomation
.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
}
}
fun chooser(matcher: Matcher<Intent>): Matcher<Intent> = allOf(
hasAction(Intent.ACTION_CHOOSER),
hasExtra(`is`(Intent.EXTRA_INTENT), matcher)
)
错误:
junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: (has action: is "android.intent.action.CHOOSER" and has extras: has bundle with: key: is "android.intent.extra.INTENT" value: (has action: is "android.intent.action.SEND" and has type: is "text/plain" and has extras: has bundle with: key: is "android.intent.extra.TEXT" value: is "Check out the Apple plant in the Android Sunflower app"))
Matched intents:[]
Recorded intents:
-Intent { act=android.intent.action.CHOOSER flg=0x8080000 (has extras) } handling packages:[[android]], extras:[Bundle[{android.intent.extra.INTENT=Intent { act=android.intent.action.SEND typ=text/plain flg=0x80001 (has clip) (has extras) }}]])
at junit.framework.Assert.fail(Assert.java:50)
at androidx.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:80)
at androidx.test.espresso.intent.Intents.internalIntended(Intents.java:346)
at androidx.test.espresso.intent.Intents$3.run(Intents.java:204)
at androidx.test.espresso.intent.Intents$PropogatingRunnable.run(Intents.java:224)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1885)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5931)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)