我的Android gradle和robolectric单元测试运行正常,直到我必须为使用JSONException或org.json包中的任何内容的类编写单元测试。
apply plugin: 'android'
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
apply plugin: 'crashlytics'
apply plugin: 'jacoco' //provides code coverage metrics
dependencies {
// regular dependences
...
// unit testing
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
androidTestCompile('com.squareup:fest-android:1.0.+') {
exclude group: 'com.android.support'
}
androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
}
我得到的例外情况如下,这很奇怪,因为所有构建都很好。任何的想法?我对robolectric的排除是否有所帮助?
java.lang.NoClassDefFoundError: org/json/JSONException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
at java.lang.Class.getConstructors(Class.java:1522)
at org.junit.runners.model.TestClass.<init>(TestClass.java:39)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:75)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:62)
...
我知道为什么会碰到这个?