Gal Ben-Haim
Gal Ben-Haim

Reputation: 17803

Intellij IDEA: NoClassDefError in Instrumentation test of a SherlockActivity, but working from Maven commandline

I'm trying to setup an instrumentation test for an Activity which extends SherlockActivity, it compiles (and runs) but fails with NoClassDefError exception:

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)

I'm using Maven and got ActionBarSherlock as a dependency in the test project's pom.xml:

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

this the test case:

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 {

    }
}

its worth mentioning that I can successfully run other tests and also this one if I change the activity to a non-Sherlock activity.

also, building and running the test project from maven command line works and the tests are running on the emulator and passing !

how can I fix this ?

Upvotes: 0

Views: 740

Answers (1)

Gal Ben-Haim
Gal Ben-Haim

Reputation: 17803

apparently the problem was with the ActioBarSherlock dependency marked as 'compile' in the test project's dependencies list. (in pom.xml its set to 'provided')

changing it to 'provided' fixed the issue.

Upvotes: 4

Related Questions