Issue
How can I test this class in Android to verify that it does indeed open up an email sender app picker, and when an app is selected, the fields are pre-filled and the file is attached?
Should it be a unit test or an integration test or an automated test through the UI. What kind of set up do I need and how can I test only this class in isolation:
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..."));
}
}
Solution
You can try unit testing this with the help of Robolectric. When you call the method sendEmailWithAttachment, you can check whether the intent performed the job of starting the email sending app,
ShadowActivity shadowActivity = shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertThat(shadowIntent.getComponent().getClassName(), equalTo(targetActivityName));
Also you can verify the content of the intent.
For more details on how to use Robolectric you can refer http://www.vogella.com/tutorials/Robolectric/article.html
Answered By - Neh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.