3

我尝试设置 robolectric 以使用针对 Google API 目标编译的项目,但无法使其正常工作。

我尝试了各种方法并使用deckard-gradle隔离了问题:

当我下载 Deckard 项目时,一切正常,示例测试成功运行。但是,当我将 gradle 文件中的 compileSdk 设置更改为 Google Inc.:Google APIs:19 时,我在运行测试时收到此 AnnotationFormatError :

   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)

以下是我的设置中的一些相关片段:

构建.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')
        }
    }

我的测试课:

@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);
    }
}

我还按照robolectric的说明将地图和支持库安装到我的本地 maven 存储库中。

任何想法在我的设置中可能有什么问题?

4

2 回答 2

0

由于各种原因,我放弃了上述方法,转而将我的 Robolectric 测试保存在 Gradle 子模块中。你可以找到我写的一篇关于我为什么朝这个方向移动的博客文章,以及一个向你展示如何在这里的 Deckard-gradle 项目的分支。由于 Robolectric 仅支持 SDK 版本 18 及更低版本,因此该项目还有一个修改后的 RobolectricTestRunner,它可以强制执行此操作,而无需注释来模拟较低的 SDK 版本。

于 2014-12-05T20:34:34.507 回答
0

对我来说的问题是,compileSdkVersion 在我的 gradle 文件中设置为 21,并且由于 Robolectric 不正式支持 21(他们使用现在处于测试阶段的版本 3)我将其更改为 18,它现在可以工作了。

于 2015-03-03T15:07:13.417 回答