Android测试java.lang.NoClassDefFoundError由于Fest-Android导致的错误

我目前正在为我的项目实现Android的Fest,但我似乎遇到了依赖问题.如果我在没有包含Fest库的情况下运行测试,测试将正常运行.一旦我添加了Fest库,那么测试就不再运行了.而是抛出异常.

我的项目使用以下依赖项:

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:+'

我已经尝试排除我在下面列出的Fest Android依赖项,但它对测试的运行没有影响.

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'
}

这是在包含Fest库的情况下运行测试时发生的异常.

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)

任何帮助或建议将不胜感激.

最佳答案
你有两个问题:

>您正在为项目编译两个版本的robotium,因此您可以删除其中一个版本.
> FEST-Android已经依赖于support-v4库,因此您需要排除这一点,就像您从’dexmaker-mockito’依赖项中排除’hamcrest-core’一样.您可以从FEST-Android 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:+')

正如您所看到的,我已经删除了您拥有的额外robotium依赖项,并且我还排除了模块“support-v4”.希望这应该让你立刻回来运行测试! 😀

转载注明原文:Android测试java.lang.NoClassDefFoundError由于Fest-Android导致的错误 - 代码日志