android - 无法在 Android Espresso 测试中获取正在测试的 Activity

标签 android android-testing android-espresso

在我们的 Espresso 测试中,我们需要自定义启动 Intent 以传递自定义附加项等。因此,我们将 launchActivity 标志设置为 false:

@Rule
public final ActivityTestRule<CreateQuoteActivity> mActivityRule = new ActivityTestRule<>(
        CreateQuoteActivity.class, true, false
);

现在,我想获得对被测 Activity 的引用。如果该标志为 true,我将使用 mActivityRule.getActivity()。但是,现在 mActivityRule.getActivity() 返回 null

我怎样才能获得对 Activity 的引用?

最佳答案

如果您已将 launchActivity 设置为 false,则只有在您真正开始并启动它时才能访问该 Activity 。

因此,您的 Activity 上下文就在那里:

final CreateQuoteActivity activity = mActivityRule.launchActivity(mIntent);

关于android - 无法在 Android Espresso 测试中获取正在测试的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32705993/

相关文章:

java - 从RadioButton传递字符串android :text ="@string/blue" to the next activity in android

Android 导航组件 Unresolved reference

android - 在 Android 中过滤和数组适配器后 ListView 未刷新 - 请帮助

android - Robolectric Android- 在 editTextField 上测试输入

android - 确定 Espresso 测试时的 fragment 变化

java - 从activity外部接收Android Activity事件?

android - 如何为 android 架构组件生命周期事件添加单元测试?

android - 在演示者类中测试协程

java - 如何获取 Espresso 中的元素列表?

android - 无法使用 Espresso 从 AutoCompleteTextView 下拉列表中选择项目