我正在尋找一個解決方案,使我的測試(這實際上是一個ActivityInstrumentationTextCase2)測試我的活動的onActivityResults方法有一個特殊的(模擬)請求 -/resultCode爲和意圖...Android的測試onActivityResult
代碼: 方法應該被測試:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
//In this case the user selected an image from his harddrive
case StartupActivity.PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
this.processSelectedFile(data.getData());
}
break;
}
}
測試類:
public void testOnActivityResult() {
//here I would like to call the onActivityResult method from my mActivity object.
}
您是否嘗試過設置你的活動對象,然後只調用onActivityResult? – weston 2012-02-08 12:22:20
嘿感謝您的答案 - onActivityResult是一種受保護的方法,因此在測試類中不可見... – Vossi 2012-02-08 12:27:43
你問你應該在你的「testOnActivityResult()」方法中寫什麼代碼?或者你問你的「onActivityResult」方法是否按你想要的方式進行,或者如何驗證數據已經被處理?我會想象@韋斯頓建立和調用該方法的建議將是最快的測試方法,如果它正在做你想做的事情。或者你是否希望實現更多自動化測試(Junit等)? – breadbin 2012-02-08 12:31:25