gpt4 book ai didi

android - Activity 因缺少 Junit ActivityTestRule 参数而崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:13:01 26 4
gpt4 key购买 nike

我有一个用于 UI 测试的 Espresso 测试套件,如下所示:

@RunWith(AndroidJUnit4.class)
public class SpecialUiTests {

@Rule
public final ActivityTestRule<SpecialActivity> activity
= new ActivityTestRule<>(SpecialActivity.class);

@Test
public void specialTest() {
...
}

...

}

问题是,该 Activity 需要一个包,并在找不到它期望的值时崩溃

public class SpecialActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

final String specialValue = getIntent().getBundleExtra(ARG_SPECIAL_BUNDLE)
.getString(KEY_SPECIAL_VALUE);

//Do something with specialValue <--- Crash

}

...

}

我可以设置一个测试规则并仍然传递 Activity 期望的参数(一个包)吗?

最佳答案

@Rule
public ActivityTestRule activityRule = new ActivityTestRule<>(
SpecialActivity.class,
true, // initialTouchMode
false); //Lazy launching

@Test
public void specialTest() {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(SpecialActivity.KEY_SPECIAL_VALUE, "789");
intent.putExtra(SpecialActivity.ARG_SPECIAL_BUNDLE, bundle);
activityRule.launchActivity(intent);

onView(withId(R.id.special))
.check(matches(withText("789")));
}

来源:http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html

关于android - Activity 因缺少 Junit ActivityTestRule 参数而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845038/

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