Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有Play服务的Robolectric 3中的异步测试代码_Android_Junit_Robolectric - Fatal编程技术网

Android 具有Play服务的Robolectric 3中的异步测试代码

Android 具有Play服务的Robolectric 3中的异步测试代码,android,junit,robolectric,Android,Junit,Robolectric,我在OkHttp模拟Web服务器和Google Play服务中使用Robolectric 3 rc2 我需要使用模拟服务器测试json的解析以及与之相关的相关处理,服务器客户端已经在使用它了。整个服务器客户端调用正在后台运行okhttp异步调用。如果是库项目,则为整个项目 完整代码 渐变配置: testCompile 'org.apache.maven:maven-ant-tasks:2.1.3' testCompile ("org.robolectric:robolectric:3.

我在OkHttp模拟Web服务器和Google Play服务中使用Robolectric 3 rc2

我需要使用模拟服务器测试json的解析以及与之相关的相关处理,服务器客户端已经在使用它了。整个服务器客户端调用正在后台运行okhttp异步调用。如果是库项目,则为整个项目

完整代码

渐变配置:

testCompile 'org.apache.maven:maven-ant-tasks:2.1.3'
testCompile ("org.robolectric:robolectric:3.0-rc2"){
    exclude module: 'commons-logging'
    exclude module: 'httpclient'
}
testCompile 'com.google.android.gms:play-services:7.0.0'
testCompile ("org.robolectric:shadows-play-services:3.0-rc2"){
    exclude module: 'commons-logging'
    exclude module: 'httpclient'
}
testCompile 'org.robolectric:shadows-support-v4:3.0-rc2'
testCompile 'org.assertj:assertj-core:1.7.0'
testCompile 'com.squareup.okhttp:mockwebserver:1.2.1'
我正在运行测试为什么命令行:

./gradlew mymodule:clean mymodule:assembleDebug mymodule:testDebug--info

错误 我遇到了一个与google play services相关的错误
java.lang.IllegalStateException:从主线程调用它可能会导致死锁

我在做广告的时候肯定做错了什么

完整堆栈跟踪:

java.lang.RuntimeException: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
    at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:57)
    at org.robolectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:93)
    at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:218)
    at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:73)
    at org.robolectric.util.Scheduler.post(Scheduler.java:60)
    at org.robolectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:90)
    at android.os.AsyncTask.execute(AsyncTask.java)
    at com.example.sdk.adServer.ServerClient.<init>(ServerClient.java:101)
    at com.example.sdk.ServerClientTest.testLongTask(ServerClientTest.java:123)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:235)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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:64)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    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:106)
    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:745)
Caused by: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
    at com.google.android.gms.common.internal.zzx.zzbe(Unknown Source)
    at com.google.android.gms.common.internal.zzx.zzbe(Unknown Source)
    at com.google.android.gms.ads.identifier.AdvertisingIdClient.finish(Unknown Source)
    at com.google.android.gms.ads.identifier.AdvertisingIdClient.finish(Unknown Source)
    at com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source)
    at com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source)
    at com.example.sdk.adServer.ServerClient$RetrieveAdvertisingId.doInBackground(ServerClient.java:459)
    at com.example.sdk.adServer.ServerClient$RetrieveAdvertisingId.doInBackground(ServerClient.java:452)
    at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:144)
    at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:241)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
    at org.robolectric.shadows.ShadowAsyncTaskBridge.doInBackground(ShadowAsyncTaskBridge.java:20)
    at org.robolectric.shadows.ShadowAsyncTask$BackgroundWorker.call(ShadowAsyncTask.java:147)
    at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:52)
java.lang.RuntimeException:java.lang.IllegalStateException:从主线程调用此函数可能导致死锁
位于org.roblectric.util.SimpleFuture.run(SimpleFuture.java:57)
位于org.roblectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:93)
位于org.roblectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:218)
位于org.roblectric.util.Scheduler.postdayed(Scheduler.java:73)
位于org.roblectric.util.Scheduler.post(Scheduler.java:60)
位于org.roblectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:90)
在android.os.AsyncTask.execute(AsyncTask.java)中
位于com.example.sdk.adServer.ServerClient.(ServerClient.java:101)
位于com.example.sdk.ServerClientTest.testLongTask(ServerClientTest.java:123)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:44)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:41)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
位于org.junit.internal.runners.statements.runafter.evaluate(runafter.java:31)
位于org.roblectric.roblectrictTestRunner$2.evaluate(roblectrictTestRunner.java:235)
位于org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
位于org.roblectric.roblectrictTestRunner$1.evaluate(roblectrictTestRunner.java:168)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:236)
位于org.gradle.api.internal.tasks.testing.junit.junitsetclassexecuter.runTestClass(junitsetclassexecuter.java:86)
位于org.gradle.api.internal.tasks.testing.junit.junitsetclassexecuter.execute(junitsetclassexecuter.java:49)
位于org.gradle.api.internal.tasks.testing.junit.junitestclassprocessor.processTestClass(junitestclassprocessor.java:64)
位于org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
位于org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
位于org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
位于org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
位于org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
位于com.sun.proxy.$Proxy2.processTestClass(未知源)
位于org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
位于org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
位于org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
位于org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
位于org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
运行(Thread.java:745)
原因:java.lang.IllegalStateException:从主线程调用此函数可能导致死锁
位于com.google.android.gms.common.internal.zzx.zzbe(未知来源)
位于com.google.android.gms.common.internal.zzx.zzbe(未知来源)
在com.google.android.gms.ads.identifier.advisingiclient.finish(未知来源)
在com.google.android.gms.ads.identifier.advisingiclient.finish(未知来源)
在com.google.android.gms.ads.identifier.advisingidclient.getadvisingidinfo(未知来源)
在com.google.android.gms.ads.identifier.advisingidclient.getadvisingidinfo(未知来源)
位于com.example.sdk.adServer.ServerClient$RetrieveAdvertisingId.doInBackground(ServerClient.java:459)
位于com.example.sdk.adServer.ServerClient$RetrieveAdvertisingId.doInBackground(ServerClient.java:452)
位于org.roblectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:144)
位于org.roblectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:241)
位于org.roblectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
位于org.roblectric.shadows.ShadowAsyncTaskBridge.doInBackground(ShadowAsyncTaskBridge.java:
java.lang.RuntimeException: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            AdvertisingIdClient.Info idInfo;
            String advertId = null;
            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                advertId = idInfo.getId();
            } catch (GooglePlayServicesNotAvailableException|GooglePlayServicesRepairableException|IOException e) {
                e.printStackTrace();
            }
            return advertId;
        }

        @Override
        protected void onPostExecute(final String advertId) {
            KakaoTaskQueue.getInstance().addTask(new KakaoResultTask<EventsLogResponse>(callback) {
                @Override
                public EventsLogResponse call() throws Exception {
                    if (advertId == null)
                        throw new IllegalAccessException("Could not get adid from the device.");
                    rootEvent.setFrom(advertId);
                    return EventsApi.requestLoggingEvents(rootEvent, leafEvents);
                }
            });
        }
    };
    task.execute();