4

My Android gradle and robolectric unit tests were running fine until I had to write unit tests for a class that used JSONException or anything from the org.json package.

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

The exception that I am getting is the following which is weird because all the builds are fine. Any idea? Does it have something to so with my exclusions for 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)
    ...

Any idea why I would run into this?

lazypig
  • 747
  • 1
  • 6
  • 22
  • Or `org.json` is excluded or partialy hidden by some dependencies from lib folder – Eugen Martynov Dec 29 '14 at 13:43
  • Actually why you have `apply 'android'` and `apply 'com.android.application'`. You should use only one, the last one with latest android gradle plugin. Also why so many exclusions for robolectric dependency? – Eugen Martynov Dec 29 '14 at 13:46
  • @EugenMartynov the exclusions are the same in [deckard-gradle](https://github.com/robolectric/deckard-gradle/blob/master/build.gradle) – Erik B Feb 12 '15 at 01:41
  • I found the answer in another post: https://stackoverflow.com/questions/3951274/how-to-unit-test-json-parsing/29684901 It worked for me. – Wenlu Apr 26 '19 at 18:39

0 Answers0