简体   繁体   English

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

[英]Android Testing java.lang.NoClassDefFoundError Error due to Fest-Android

I'm currently implementing Fest for Android into my project but I seem to be running into a dependency issue. 我目前正在为我的项目实现Android的Fest,但我似乎遇到了依赖问题。 If I run my tests without the Fest library included, the tests will run as normal. 如果我在没有包含Fest库的情况下运行测试,测试将正常运行。 Once I add in the Fest library then the tests no longer run. 一旦我添加了Fest库,那么测试就不再运行了。 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. 我已经尝试排除我在下面列出的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'
}

This is the exception that is occurring when the tests are run with the Fest library included. 这是在包含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)

Any help or suggestions would be greatly appreciated. 任何帮助或建议将不胜感激。

You have two problems here: 你有两个问题:

  1. You are compiling two versions of robotium into your project, so you can remove one of them. 您正在为项目编译两个版本的robotium,因此您可以删除其中一个版本。

  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. FEST-Android已经依赖于support-v4库,因此您需要排除这一点,就像您从'dexmaker-mockito'依赖项中排除'hamcrest-core'一样。 You can see all of its dependencies from FEST-Android Dependencies . 您可以从FEST-Android依赖项中查看其所有依赖项

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' . 正如您所看到的,我已经删除了您拥有的额外robotium依赖项,并且我还排除了模块“support-v4” Hopefully this should have you back running tests in no time! 希望这应该让你立刻回来运行测试! :-D :-D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM