java.lang.NoClassDefFoundError运行android单元测试时用ActivityInstrumentationTestCase2测试FragmentActivity

时间:2012-09-24 10:24:31

标签: android testing android-fragmentactivity

我正在尝试使用ActivityInstrumentationTestCase2测试从FragmentActivity派生的Activity(来自android-support-v4.jar)。 android-support-v4.jar位于我的主项目(测试项目)的libs文件夹中。为了使测试项目通过构建,我添加了一个从主项目到测试项目的链接,命令为“ln ../mainProject/libs/android-support-v4.jar ./libs/android-support-v4”。 jar“在我的测试项目根目录中。 http://dtmilano.blogspot.com/2009/12/android-testing-external-libraries.html?showComment=1348480869133建议使用它。按照博客的指南,我可以成功运行Eclipse测试。 但是,当我尝试使用命令“ant clean debug install test”运行测试时,我将始终遇到以下异常:

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

以下是测试代码的快照:

 public class QuestionListActivityTest extends ActivityInstrumentationTestCase2<QuestionListActivity>{
    private Solo solo;
    private FragmentActivity activity;

 public QuestionListActivityTest() {  
     super(QuestionListActivity.class); **// the exception happened here**
 }
...
}

我正在使用android sdk R15。测试是使用模拟器运行的。 有人知道怎么解决吗?我真的需要用ant来运行它以便与hudson集成。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

您是否检查了相关问题here

与你的相似。