我尝试测试每次应用启动时启动特殊活动的应用。让我们调用此活动AlreadyStartedActivity
。测试用例看起来像这样:
public class AlreadyStartedActivityTest extends ActivityInstrumentationTestCase2<AlreadyStartedActivity> {
public AlreadyStartedActivityTest() {
super(AlreadyStartedActivity.class);
}
public void testStart() {
Solo solo = new Solo(getInstrumentation(), getActivity());
// use solo to click and test some features of the activity.
}
}
问题是AlreadyStartedActivity
中的AndroidManifest.xml
配置如下:
<activity android:name=".activity.AlreadyStartedActivity"
android:taskAffinity=""
android:excludeFromRecents="true"
android:launchMode="singleTop" />
singleTop
避免调用AlreadyStartedActivity#onCreate
,这会永久阻止getActivity
方法。
我能做些什么来避免这种情况吗?
问候
答案 0 :(得分:0)
看起来您没有正确设置测试类。您需要添加默认构造函数:
public class AlreadyStartedActivityTest extends
ActivityInstrumentationTestCase2<AlreadyStartedActivity> {
public AlreadyStartedActivityTest() {
super(AlreadyStartedActivity.class);
}
public void testStart() {
Solo solo = new Solo(getInstrumentation(), getActivity());
// use solo to click and test some features of the activity.
}
}
最好将Solo
移至setUp()
方法。