1

I am migrating my project to Gradle and I had a separate working Robolectric setup along with my App.

The Robolectric tests are under the test/ folder and they should include the app jar file (thanks to another SO question) and other dependecies at runtime.

The tests are launched by Gradle, but the classpath looks empty.

The gradle.build:

apply plugin: 'java'

test {
   // The classpath.each prints:
   // /test-robolectric/build/classes/test
   // /test-robolectric/build/resources/test
   // /test-robolectric/build/classes/main
   // /test-robolectric/build/resources/main

    classpath.each { file ->
        println file.absolutePath
    }     

    reports {
        html.enabled = true
    }

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = false

    // listen to events in the test execution lifecycle
    beforeTest { descriptor ->
        logger.lifecycle("Running test: " + descriptor)
    }
}

dependencies {
    compile project(':test-common:test-common-jar')
    compile project(path: ':app', configuration: 'testJar')
    compile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    compile 'com.google.android:android:4.+'
    compile 'com.android.support:appcompat-v7:19+@jar'
    compile 'com.android.support:support-v4:19.+'
    compile 'com.google.inject:guice:4.0-beta:no_aop'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'org.functionaljava:functionaljava:3.1'
    compile 'javax.inject:javax.inject:1@jar'
    compile 'com.squareup:otto:1.3.4@jar'
    compile 'net.jcip:jcip-annotations:1.0'
    compile 'de.akquinet.android.androlog:androlog:1.0.5'
    compile 'com.j256.ormlite:ormlite-android:4.48'
    compile 'com.j256.ormlite:ormlite-core:4.48'
    compile 'junit:junit:4.11'
    compile 'org.mockito:mockito-all:1.9.+'
    compile 'org.hamcrest:hamcrest-core:1.+'
    compile 'com.squareup:fest-android:1.0.+'
}

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url 'http://repository.grepcode.com/java/ext'
    }
}

I have also tried to put the dependecies declaration inside the test{} section to no avail. What am I missing here?

The specific exception:

java.lang.NoClassDefFoundError: android/content/Context
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
[...]
Community
  • 1
  • 1
Andrea Richiardi
  • 703
  • 6
  • 21

0 Answers0