【问题标题】:java.lang.NoClassDefFoundError when running android unit test to test FragmentActivity with ActivityInstrumentationTestCase2运行android单元测试以使用ActivityInstrumentationTestCase2测试FragmentActivity时出现java.lang.NoClassDefFoundError
【发布时间】:2013-01-11 20:38:41
【问题描述】:

我正在尝试使用 ActivityInstrumentationTestCase2 测试从 FragmentActivity(来自 android-support-v4.jar)派生的 Activity。 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.在测试代码中使用Activity而不是FragmentActivity。 2.在测试项目的编译阶段(ant debug)添加链接(ln),然后在测试运行阶段(ant install test)删除链接。然后测试用例就可以成功运行了。

标签: android testing android-fragmentactivity


【解决方案1】:

你检查过相关问题here吗?

和你的很相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多