0

我正在尝试为Activitywhich extends设置一个仪器测试SherlockActivity,它编译(并运行)但失败并出现NoClassDefError异常:

junit.framework.AssertionFailedError: Exception in constructor: testPreconditions     (java.lang.NoClassDefFoundError: com.dgti.ds.activities.ChooseLocationActivity
at com.dgti.ds.test.activities.ChooseLocationActivityTest.<init>(ChooseLocationActivityTest.java:11)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:103)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:127)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:55)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:81)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:418)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:399)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:364)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3246)
at android.app.ActivityThread.access$2200(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(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:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

我正在使用 Maven 并ActionBarSherlock作为测试项目的 pom.xml 中的依赖项:

<dependency>
  <groupId>com.actionbarsherlock</groupId>
  <artifactId>actionbarsherlock</artifactId>
  <version>4.2.0</version>
  <scope>provided</scope>
  <type>apklib</type>
</dependency> 

这是测试用例:

public class ChooseLocationActivityTest extends ActivityInstrumentationTestCase2<ChooseLocationActivity> {

    public ChooseLocationActivityTest() {
        this(ChooseLocationActivity.class);
    }

    public ChooseLocationActivityTest(Class<ChooseLocationActivity> activityClass) {
        super(activityClass);
    }

    public void setUp() throws Exception {

    }

    public void tearDown() throws Exception {

    }

    public void testPreconditions() throws Exception {

    }
}

值得一提的是,如果我将活动更改为非 Sherlock 活动,我可以成功运行其他测试和这个测试。

此外,从 maven 命令行构建和运行测试项目可以工作,并且测试正在模拟器上运行并通过!

我怎样才能解决这个问题 ?

4

1 回答 1

4

显然问题出在测试项目的依赖项列表中标记为“编译”的 ActioBarSherlock 依赖项。(在 pom.xml 中其设置为“提供”)

将其更改为“提供”解决了该问题。

于 2012-11-04T14:52:34.513 回答