Android studio 在Android studio 1.2中运行robolectric 3.0单元测试时出现运行时异常

Android studio 在Android studio 1.2中运行robolectric 3.0单元测试时出现运行时异常,android-studio,robolectric,robolectric-gradle-plugin,Android Studio,Robolectric,Robolectric Gradle Plugin,我正在使用Robolectric 3.0运行android单元测试,并且遇到了这个运行时异常。所有的测试几乎都给出了相同的异常 java.lang.RuntimeException: java.lang.RuntimeException: you should only be calling this from the main thread! at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunne

我正在使用Robolectric 3.0运行android单元测试,并且遇到了这个运行时异常。所有的测试几乎都给出了相同的异常

java.lang.RuntimeException: java.lang.RuntimeException: you should only be calling this from the main thread!
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    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:360)
    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:744)
Caused by: java.lang.RuntimeException: you should only be calling this from the main thread!
    at org.robolectric.shadows.ShadowLooper.resetThreadLoopers(ShadowLooper.java:52)
    at org.robolectric.Shadows.reset(Shadows.java:1617)
    at org.robolectric.Robolectric.reset(Robolectric.java:22)
    at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:43)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:233)
    ... 34 more
我尝试了许多建议的选项,例如添加--rerun任务到脚本参数,取消选中编译器->Android Studio的Gradle设置中的“使用进程内构建”选项等等,但都没有成功

以下是我的build.gradle依赖项列表示例:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.android.support:appcompat-v7:18.0.0'){
        force = true
    }
    compile ('com.android.support:mediarouter-v7:18.0.0'){
        force = true
    }
    compile 'com.google.android.gms:play-services:6.5.87'

    testCompile 'junit:junit:4.12'

    testCompile 'com.github.tomakehurst:wiremock:1.46'

    testCompile 'org.bouncycastle:bcprov-jdk15on:1.50'

    testCompile ('org.robolectric:robolectric:3.0-rc3'){
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    testCompile 'org.robolectric:shadows-support-v4:3.0'
    testCompile 'org.robolectric:shadows-httpclient:3.0'
    testCompile 'org.robolectric:shadows-maps:3.0'
}
我花了很多时间研究它,但没有结果。我非常感谢在这方面提供的任何帮助


谢谢

为什么您的阴影版本为3.0,而robolectric版本为3.0-rc3?如何通过gradle运行测试?另外wiremock是独立的web应用程序,不应在任何范围依赖中提及。您还使用非常旧的compat库,为什么要强制执行更新检查?@EugenMartynov感谢您的回复。是的,我正在通过gradle进行单元测试。至于阴影版…我的错。我已将它们全部更正为使用3.0版。我已经移除了wiremock,因为我不需要它。我的应用程序在最新的v7库支持下表现得很奇怪,因此在升级应用程序之前,现在必须使用旧版本。问题仍然存在。您是否共享外部构建脚本,甚至将项目置于公共位置?你真的使用robolectric gradle插件吗?为什么你的阴影版本是3.0,而robolectric是3.0-rc3?如何通过gradle运行测试?另外wiremock是独立的web应用程序,不应在任何范围依赖中提及。您还使用非常旧的compat库,为什么要强制执行更新检查?@EugenMartynov感谢您的回复。是的,我正在通过gradle进行单元测试。至于阴影版…我的错。我已将它们全部更正为使用3.0版。我已经移除了wiremock,因为我不需要它。我的应用程序在最新的v7库支持下表现得很奇怪,因此在升级应用程序之前,现在必须使用旧版本。问题仍然存在。您是否共享外部构建脚本,甚至将项目置于公共位置?你真的使用机器人分子梯度插件吗?