3

I tried to setup robolectric to work with my project compiled against a Google API target, but can't get it to work.

I have tried various approaches and have isolated the problem using deckard-gradle:

When I download the deckard project everything works fine and the sample test runs successfully. However when I change the compileSdk setting in my gradle file to Google Inc.:Google APIs:19 I get this AnnotationFormatError when running the test:

   java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.robolectric.annotation.Config.application()
    at java.lang.reflect.Method.getDefaultValue(Method.java:747)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:128)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:263)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:117)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3271)
    at java.lang.Class.getAnnotations(Class.java:3240)
    at org.junit.runner.Description.createSuiteDescription(Description.java:123)
    at org.junit.internal.runners.ErrorReportingRunner.getDescription(ErrorReportingRunner.java:25)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:83)
    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:48)
    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)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    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.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Here are some relevant snippets from my setup:

build.gradle

    buildscript {
        repositories {
            mavenCentral()
        }

        dependencies {
            classpath 'com.android.tools.build:gradle:0.13.+'
            classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'
        }
    }

    allprojects {
        repositories {
            mavenCentral()
        }
    }

    apply plugin: 'android'
    apply plugin: 'robolectric'

    android {
        packagingOptions {
            exclude 'LICENSE.txt'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE'
        }
        compileSdkVersion "Google Inc.:Google APIs:19"
        buildToolsVersion "19.1.0"

        defaultConfig {
            minSdkVersion 18
            targetSdkVersion 18
            versionCode 2
            versionName "1.0.0-SNAPSHOT"
            testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        }
        buildTypes {
            release {
                runProguard false
            }
        }

        sourceSets {
            androidTest {
                setRoot('src/test')
            }
        }
    }

    robolectric {
        include '**/*Test.class'
        exclude '**/espresso/**/*.class'
    }

    dependencies {
        repositories {
            mavenCentral()
        }
        // Espresso
        androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
        androidTestCompile 'com.google.guava:guava:14.0.1'
        androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
        androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
        androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
        androidTestCompile 'org.hamcrest:hamcrest-library:1.1'

        androidTestCompile('junit:junit:4.11') {
            exclude module: 'hamcrest-core'
        }
        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'
        }
        androidTestCompile 'com.squareup:fest-android:1.0.+'
    }

    apply plugin: 'idea'

    idea {
        module {
            testOutputDir = file('build/test-classes/debug')
        }
    }

My Test class:

@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class DeckardActivityRobolectricTest {

    @Test
    public void testSomething() throws Exception {
        Activity activity = Robolectric.buildActivity(DeckardActivity.class).create().get();
        assertTrue(activity != null);
    }
}

I have also followed the instructions on robolectric to install the maps and the support lib into my local maven repo.

Any ideas what might be wrong in my setup?

ct_rob
  • 521
  • 3
  • 16
  • I'm not sure but maybe you should not change compile sdk but add additional dependency `compile 'com.google.android.gms:play-services:6.1.71'` – Eugen Martynov Dec 04 '14 at 10:32

2 Answers2

0

For various reasons, I've abandoned the above approach in favor of keeping my Robolectric tests in a Gradle submodule. You can find a blog post I wrote about why I moved in this direction and a fork of the deckard-gradle project that shows you how here. Since Robolectric only support SDK version 18 and lower, this project also has a modified RobolectricTestRunner that forces this without requiring the annotation to emulate the lower SDK version.

jdonmoyer
  • 1,245
  • 1
  • 18
  • 28
  • Thanks. I couldn't get quite to the root of my problem, but I guess it was conflicting dependencies or something similar. I agree with you that it is much less painful to use a submodule. Thanks a lot for your deckard fork, it works fine with Google APIs. Of course well in line with my robolectric experiences I stumbled into a few other problems after solving this one and I'm currently stuck with the use of actionbarsherlock and it's themes. But I'll get to the top of the mountain eventually, even if they throw rocks at me ;) – ct_rob Dec 08 '14 at 13:35
  • @jdonmoyer Did you check you approach against Android Studio 1.1.0 and added unit testing support - http://tools.android.com/tech-docs/unit-testing-support ? – bogumil Feb 27 '15 at 12:04
  • @bogumil I am currently using projects based off of the example I have on github with Android Studio 1.1.0, but I haven't investigated the built in unit testing support yet. If you get it working, definitely feel free to submit a patch. – jdonmoyer Feb 27 '15 at 13:59
0

The issue for me was that compileSdkVersion in my gradle file was set to 21 and since Robolectric does not officially support 21 (they do with version 3 which is in beta right now) I changed it to 18 and it works now.

Niklas
  • 23,674
  • 33
  • 131
  • 170