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

时间:2015-07-21 23:09:03

标签: 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(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-tasks 添加到脚本参数,取消选中编译器中的“使用进程内构建”选项 - > 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'
}

花了很多时间查看它但没有结果。我非常感谢这方面的任何帮助。

谢谢!

0 个答案:

没有答案
相关问题