使用Robotium测试已在运行的Activity

时间:2013-04-09 11:56:33

标签: android robotium android-testing

我尝试测试每次应用启动时启动特殊活动的应用。让我们调用此活动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方法。

我能做些什么来避免这种情况吗?

问候

1 个答案:

答案 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()方法。