我有兩個應用程序通過意圖彼此交互。我想驗證假設應用程序A正確調用startActivity
爲應用程序B沒有實際啓動應用程序B。我嘗試過intending
和Espresso的各種組合,但仍然推出應用程序B,而不是僅僅將它剔除。這會導致其餘測試失敗,因爲UI被應用B阻止。有任何想法嗎?使用意式咖啡粉碎意圖時出錯
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyActivityUiIntentsTest
{
@Rule
public IntentsTestRule<MyActivity> activityRule =
new IntentsTestRule<>(MyActivity.class, true, false);
@Test
public void shouldStartOtherActivityWhenButtonClicked()
{
Intents.init();
intending(toPackage("my.package"))
.respondWith(new ActivityResult(Activity.RESULT_OK, null));
activityRule.launchActivity(new Intent());
onView(withId(R.id.viewId)).perform(click());
intended(hasComponent(hasShortClassName("the.other.class.name")));
Intents.release();
}
}
更新:代碼爲onClick
:
@OnClick(R.id.viewId)
public void startOtherActivity()
{
Intent intent = new Intent();
intent.setClassName("my.package", "the.other.class.name");
startActivity(intent);
finish();
}
後,我們可以看到在onclick意圖創建代碼
IntentsTestRule
將調用初始化你嗎?你有沒有在意圖上記錄包名?即,您的意圖不符合正確的意圖 – Blundell添加了'onClick'示例代碼。 – brwngrldev
你在運行什麼設備/ emu API版本? –