gpt4 book ai didi

java - robolectric 测试中的链接 Activity

转载 作者:行者123 更新时间:2023-11-28 20:20:59 25 4
gpt4 key购买 nike

我试图在交互 Activity 通信中插入我的测试,并检查例如在正确登录后,我产生了正确的 Activity (来自 2 个可能的 Activity )。

这是我的代码:

@RunWith(GuiceRobolectricJUnitRunner.class)
public class LoginActivityTest {
@Inject
private LoginActivity activity;
@Inject
private ExplorerActivity startedActivity ;
@Inject
private Context context;

private Button loginButton;
private EditText login;
private EditText password;

@Before
public void setUp() throws Exception {
activity.onCreate(null);
loginButton = (Button) activity.findViewById(R.id.identification_login_button);
login = (EditText) activity.findViewById(R.id.txtLogin);
password = (EditText) activity.findViewById(R.id.txtPassword);

}

@Online
@Test
public void shouldExploreWhenLoginIsCorrect() throws Exception {
assertNotNull(activity);
login.setText("test@test.com");
password.setText("test");
activity.setIntent(new Intent());
loginButton.performClick();
ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);
assertEquals(shadowIntent.getIntentClass(), ExplorerActivity.class);
// startedActivity.setIntent(startedIntent);
// startedActivity.onCreate(null);


}
}

我的问题是我无法从 shadowintent 中检索已启动的 Activity 。有什么办法可以实现这样的目标吗?此外,我没有看到我的探索者 Activity 的任何痕迹,我想知道 Robolectric 是否正在对每个生成过程进行沙箱处理。我真的很喜欢 robolectric 中的链式 Activity 测试示例。谢谢。

最佳答案

因为是 3 个月前,您可能已经找到了答案,如果没有,您可以在已有的基础上使用 newInstance(),然后照常继续。

ExplorerActivity explorerActivity = (ExplorerActivity) shadowIntent.getIntentClass().newInstance();
explorerActivity.setIntent(startedIntent);
explorerActivity.onCreate(null);

关于java - robolectric 测试中的链接 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10814355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com