2016-08-08 51 views
0

如何在Android中測試此類以驗證它是否確實打開了電子郵件發件人應用程序選擇器,並且選擇了應用程序時,這些字段是預填充的並且文件已附加?如何在Android中對這個類進行單元測試?

是否應該通過UI進行單元測試或集成測試或自動化測試。 設置什麼樣的了,我需要,我怎麼能在單獨測試只有這個類:

public class EmailSender { 

    public static void sendEmailWithAttachment(Context context, 
               String[] recipient, 
               String subject, 
               String attachmentFilePath) { 
     Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent .setType("vnd.android.cursor.dir/email"); 
     emailIntent .putExtra(Intent.EXTRA_EMAIL, recipient); 
     emailIntent .putExtra(Intent.EXTRA_STREAM, attachmentFilePath); 
     emailIntent .putExtra(Intent.EXTRA_SUBJECT, subject); 
     context.startActivity(Intent.createChooser(emailIntent , "Send email...")); 
    } 

} 
+1

您與Android的咖啡標籤標示的問題,但我認爲你應該使用其他工具,如UI的Automator(https://developer.android.com /training/testing/ui-testing/uiautomator-testing.html)這樣的事情或Robolectric像Neh寫的 – jeprubio

回答

1

你可以嘗試單元Robolectric的幫助下測試此。 當你調用方法sendEmailWithAttachment,您可以檢查是否意圖進行啓動電子郵件發送應用程序的工作,

ShadowActivity shadowActivity = shadowOf(activity); 
    Intent startedIntent = shadowActivity.getNextStartedActivity(); 
    ShadowIntent shadowIntent = shadowOf(startedIntent); 
    assertThat(shadowIntent.getComponent().getClassName(), equalTo(targetActivityName)); 

您也可以驗證意圖的內容。

有關如何使用Robolectric更多的細節可以參考http://www.vogella.com/tutorials/Robolectric/article.html

相關問題