-->

Android Testing java.lang.NoClassDefFoundError Err

2019-07-02 05:03发布

问题:

I'm currently implementing Fest for Android into my project but I seem to be running into a dependency issue. If I run my tests without the Fest library included, the tests will run as normal. Once I add in the Fest library then the tests no longer run. An exception is thrown instead.

My project uses the following dependencies:

compile files('libs/robotium-solo-5.1.jar')
androidTestCompile 'com.squareup:fest-android:1.0.8'
androidTestCompile 'com.google.code.gson:gson:2.2.4'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.1'
androidTestCompile 'com.google.mockwebserver:mockwebserver:20130706'
androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.0') {
    exclude module: 'hamcrest-core'
    exclude module: 'objenesis'
    exclude module: 'mockito-core'
}
androidTestCompile 'org.mockito:mockito-all:+'

I've tried excluding the Fest Android dependencies that I've listed below but it has no effect on the running of the tests.

androidTestCompile ('com.squareup:fest-android:1.0.8') {
    exclude group: 'com.google.android', module: 'android'
    exclude group: 'com.google.android', module: 'support-v4'
    exclude group: 'org.easytesting', module: 'fest-assert-core'
}

This is the exception that is occurring when the tests are run with the Fest library included.

junit.framework.AssertionFailedError: Exception in constructor: testClickActionBarItems (java.lang.NoClassDefFoundError: com.example.android.activities.SectionsActivity
at com.example.android.test.activities.SectionsEspressoTests.<init>(SectionsEspressoTests.java:21)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:148)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:56)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(GoogleInstrumentationTestRunner.java:114)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

Any help or suggestions would be greatly appreciated.

回答1:

You have two problems here:

  1. You are compiling two versions of robotium into your project, so you can remove one of them.

  2. FEST-Android already has a dependency of support-v4 library so you will need to exclude that just like you are excluding 'hamcrest-core' from the 'dexmaker-mockito' dependency. You can see all of its dependencies from FEST-Android Dependencies.

I would recommend that you make the following changes to your dependencies.

// Testing Libraries
androidTestCompile('com.squareup:fest-android:1.0.8') {
    exclude module: 'support-v4'
}
androidTestCompile('com.google.code.gson:gson:2.2.4')
androidTestCompile('com.jayway.android.robotium:robotium-solo:5.1')
androidTestCompile('com.google.mockwebserver:mockwebserver:20130706')
androidTestCompile('com.google.dexmaker:dexmaker:1.0')
androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.0') {
    exclude module: 'hamcrest-core'
    exclude module: 'objenesis'
    exclude module: 'mockito-core'
}
androidTestCompile('org.mockito:mockito-all:+')

As you can see I have removed the extra robotium dependency that you had and I have also excluded the module 'support-v4'. Hopefully this should have you back running tests in no time! :-D