NoClassDefFoundError FragmentActivity测试

时间:2013-11-24 10:36:19

标签: android android-fragments

我正在尝试创建一个简单的Android Activity测试,用于检查单击按钮时是否启动了新的Activity。代码:

public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {

public LoginActivityTest() {
    super(LoginActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();

    setActivityInitialTouchMode(false);

    mActivity = getActivity();

    mLoginButton = (Button) mActivity.findViewById(R.id.login_button);
    mSkipButton = (Button) mActivity.findViewById(R.id.skip_button);
}

{...}

public void testSkipButton() {
    Instrumentation.ActivityMonitor monitor =
    getInstrumentation().addMonitor(
        "com.mycompany.myproject.view.QuestionsActivity",  null, false);

    mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mSkipButton.requestFocus();
            mSkipButton.performClick();
        }
    });

    QuestionsActivity nextActivity =
            (QuestionsActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, 20);
    assertNotNull(nextActivity);
    nextActivity.finish();
}

private LoginActivity mActivity;
private Button mLoginButton;
private Button mSkipButton;
}

当我到达waitForMonitorWithTimeout()时,会引发NoClassDefFoundException。

重要的是要考虑到QuestionsActivity(应该启动的活动)是 FragmentActivity ,而不是Activity,但FragmentActivity继承自Activity,所以我真的不明白那里发生了什么。也许InstrumentationTest无法处理Fragments或FragmentActivities。

mActivity是当前正在测试的Activity,它是一个纯Activity。

请帮忙!

1 个答案:

答案 0 :(得分:0)

问题是我在测试项目中添加了对支持库的引用。看到这个链接: FragmentActivity can not be tested via ActivityInstrumentationTestCase2

现在测试工作正常:)