使用Eclipse测试Android会为MyActivity抛出NoClassDefFoundError

时间:2011-03-28 21:09:34

标签: android eclipse unit-testing

当我通过'android junit test'开始我的单独的android测试项目时,我总是得到:

Test run failed: Unable to find instrumentation info for: ComponentInfo{my.pack/android.test.InstrumentationTestRunner}
通过触摸AndroidManifest.xml

解决了这个问题(奇怪!!)

但现在当我尝试执行此测试时:

public class MyActivityTest extends
        ActivityInstrumentationTestCase2<MyActivity> {

    public MyActivityTest() {
        // here it fails with NoClassDefFoundError for MyActivity
        super("my.pack", MyActivity.class);
    }

    public void testGetChangedTweetIds() {
       // some tests here
    }
}

我得到了:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at my.pack.MyActivityTest.<init>(MyActivityTest.java:13)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: my.pack.MyActivity
... 19 more

虽然我已将主项目添加到测试项目的构建路径(通过eclipse配置)。将测试类放在我的主项目中(+提供和)它可以工作。

为什么eclipse在单独测试项目的推荐方法中存在这些问题?

1 个答案:

答案 0 :(得分:1)

  1. 您的第一个问题表明仪器不存在,以获取可用仪器列表

    $ adb shell pm list instrumentation

  2. public ActivityInstrumentationTestCase2 (String pkg, Class activityClass)自API级别8

  3. 后被删除
  4. 如果Eclipse安装有问题,请从命令行运行测试

    $ adb shell am instrument [flags]