如何在 Android 中对 class 进行单元测试?
How do I unit-test this class in Android?
我如何在 Android 中测试这个 class 以验证它确实打开了一个电子邮件发件人应用程序选择器,并且当一个应用程序被选中时,这些字段是预填充的并且文件已附加?
通过UI应该是单元测试还是集成测试还是自动化测试。
我需要什么样的设置以及如何单独测试这个 class:
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..."));
}
}
您可以尝试在 Robolectric 的帮助下对此进行单元测试。
当你调用sendEmailWithAttachment方法时,你可以检查intent是否执行了启动邮件发送应用程序的工作,
ShadowActivity shadowActivity = shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertThat(shadowIntent.getComponent().getClassName(), equalTo(targetActivityName));
也可以验证intent的内容
更多使用方法Robolectric可以参考http://www.vogella.com/tutorials/Robolectric/article.html
我如何在 Android 中测试这个 class 以验证它确实打开了一个电子邮件发件人应用程序选择器,并且当一个应用程序被选中时,这些字段是预填充的并且文件已附加?
通过UI应该是单元测试还是集成测试还是自动化测试。 我需要什么样的设置以及如何单独测试这个 class:
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..."));
}
}
您可以尝试在 Robolectric 的帮助下对此进行单元测试。 当你调用sendEmailWithAttachment方法时,你可以检查intent是否执行了启动邮件发送应用程序的工作,
ShadowActivity shadowActivity = shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertThat(shadowIntent.getComponent().getClassName(), equalTo(targetActivityName));
也可以验证intent的内容
更多使用方法Robolectric可以参考http://www.vogella.com/tutorials/Robolectric/article.html