如果AsyncTask
由按鈕上的點擊事件觸發,我該如何測試它 - 我如何等待AsyncTask完成?如何測試點擊觸發的AsyncTask?
注意我總是可以直接在我的測試方法中執行AsyncTask
,我知道如何測試這種情況。但是,如果我堅持使用使用performClick()
來模擬onClick
事件,我仍然可以測試我的註冊過程嗎?
MainActivityFunctionalTest
public class MainActivityFunctionalTest extends
ActivityInstrumentationTestCase2<MainActivity> {
// ...
public void testRegistration() {
ImageButton submitBtn = (ImageButton) solo.getView(R.id.BtnR);
assertNotNull(submitBtn);
submitBtn.performClick();
// How to do something when the registration is done?
}
// ...
}
MainActivity(項目進行測試)
ImageButton submitBtn = (ImageButton) findViewById(R.id.BtnRegister);
submitBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendRegistration();
}
});
public void sendRegistration() {
Processor processor = new Processor();// subclass of AsyncTask
processor.execute();
}
謝謝,但這似乎是'CountDownLatch'的替代方案。我的挑戰是測試通過'onClick'事件間接觸發的'AsyncTask'。您基本上都告訴我在我的測試類中執行'AsyncTask' - 繞過按鈕 – ericn
的onClick事件,您可以在oncreate中創建asyn對象,然後在onclick方法中執行asyntask並調用方法名稱,如' waitForMe「,它包含這個等待asyn完成的while循環的定義。 –
我相信這裏有一個很大的誤解。請結帳我的編輯:) – ericn