在我们的 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/