我正在开发和android应用程序。我正在使用reverfit(与OkClient一起使用)来处理api请求,并使用Robolectric来进行测试。我的api如下所示:
@GET("/v1/book/{bookId}") Observable<Book> getBook(@Path("bookId") int bookId);
RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(environment.getServerEndpoint())
.setClient(new OkClient(client))
.setExecutors(new ImmediateExecutor(), null)
.setErrorHandler(new ErrorHandler())
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
// Always ask for JSON data
request.addHeader("Accept", "application/json");
request.addHeader("Content-Type", "application/json");
}
});
public class ImmediateExecutor implements Executor {
@Override public void execute(Runnable command) {
command.run();
}
}
API.getBook(1).subscribe();
API.getBook(2).subscribe();
java.io.InterruptedIOException
at okio.Timeout.throwIfReached(Timeout.java:146)
at okio.Okio$1.write(Okio.java:75)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.flush(RealBufferedSink.java:201)
at com.squareup.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:140)
at com.squareup.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:828)
at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823)
at com.carmacarpool.carmaridepool.rest.CarmaHttpClientFactory$NetworkLoggingInterceptor.intercept(CarmaHttpClientFactory.java:77)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:803)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684)
at com.squareup.okhttp.Call.getResponse(Call.java:272)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.carmacarpool.carmaridepool.rest.CarmaHttpClientFactory$LoggingInterceptor.intercept(CarmaHttpClientFactory.java:53)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:225)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.execute(Call.java:79)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
at retrofit.RxSupport$2.run(RxSupport.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at com.carmacarpool.carmaridepool.testutils.ShadowUICarmaRESTClassFactory$ImmediateExecutor.execute(ShadowUICarmaRESTClassFactory.java:91)
at retrofit.RxSupport$1.call(RxSupport.java:42)
at retrofit.RxSupport$1.call(RxSupport.java:32)
at rx.Observable.subscribe(Observable.java:7393)
at rx.Observable.subscribe(Observable.java:7083)
at com.carmacarpool.carmaridepool.CorridorTest.test(CorridorTest.java:99)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:265)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:205)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:173)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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 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:106)
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:745)
有人知道问题是什么吗?似乎由于某种未知的原因,线程被杀死了?
多谢了。
编辑:如果在onNext函数(subscribe的第一个参数)中,我执行第二个请求,那么它就工作了。一切都像预期的那样是同步的。
try {
// Perform the request
return chain.proceed(request);
} catch (IOException e) {
// Retry again if we haven't tried at least REQUEST_RETRIES times
if (iteration < REQUEST_RETRIES) {
return performRequest(chain, ++iteration);
}
// Otherwise, save the exception and throw it later
exception = e;
}
希望这有帮助。
我认为您做得太多了,无法在测试期间同步运行可观察到的东西。相反,您需要做的只是在测试期间
Book book = retrofitAPI.getBook(someId).toBlocking.first()
这将同步执行observable。Rx图书馆中我最喜欢的部分之一。
我用Robolectric测试写了一个简单的hello-world。 我已经向build.gradle添加了正确的依赖项: 下面是我要测试的简单: 我运行的是Android Studio2.0预览版3B。 问题是:如何避免失败?
我试图将jBPM 5.4集成到现有的servlet中。 servlet在JBoss 7.1中运行良好,但在我添加一个与jBPM相关的API时,由于抛出异常而失败。(我使用了安装中的评估样本作为模板。)我向servlet的doPost()中添加了: 执行此语句后,将引发以下异常: 20:51:06394错误[org.apache.catalina.core.ContainerBase.[jboss.
我有以下RxJava2 Kotlin代码: 这模拟了一个可完全观察的对象(想象对API的一个简单更新操作),然后是一个可观察的对象(图像是API上的一个get操作)。我希望以一种方式连接这两个可观察对象,当Completable完成时,运行单个对象,最后在我的观察者(Int 42)上获得onSuccess事件。 但是,此测试代码不起作用。断言失败,出现以下错误: 我无法理解我做错了什么,我希望Co
我仍在尝试将RXJava2用于多个订阅者使用的轮询服务。它工作得很好,除了它总是调用两次。 我尝试使用publish(1)、take(1)、share()、refCount()等,但结果总是一样。叫两次或多次。 我还是不明白为什么它叫了两次。新订阅服务器应该接收最新发出的值,并且只在更改后的值(如果hashmap更改了)返回为modified List。 我的民意调查可观察到(单例) 我的谓词用于
我试图为我的值类构建一个自定义反序列化器类。但是ObjectMapper. readValue会被多次调用,直到抛出StackOverflow异常。以下只是一个例子: Maven依赖: 引发以下StackOverflowException: 我尝试通过模块注册反序列化程序并删除@JsonAnnotation,但仍然引发相同的异常: 然后我尝试在我的UserDeserializer中创建一个新的Ob
当第二次打开我的SupportMapFragment(Android maps v2)时(调用setContentView),我得到以下错误: 我不知道这个错误是什么意思。有人能解释一下吗?