我正在实现 mockWebServer,它适用于 200 次调用,但是当我尝试获取异常时,我得到了这个异常:
线程“OkHttp Dispatcher”中的异常 java.lang.Error: com.myproject.something.errors.MyException$ServerUnavailable at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1155) at java.util.concurrent.ThreadPoolExecutor$ Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.java:748)
和这个
引起:com.myproject.something.errors.ServerException$ServiceUnavailable at com.myproject.something.errors.MyException$ServerUnavailable.(ServerException.kt:6) at com.myproject.ErrorInterceptor.intercept(IntegrationTest.kt:86)
我有这个错误拦截器用于假改造
class ErrorInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response =
try {
val response = chain.proceed(chain.request())
if (!response.isSuccessful) {
when (response.code) {
HttpURLConnection.HTTP_UNAVAILABLE -> throw MyException.ServiceUnavailable
else -> throw ServerException(IllegalStateException("Not handled"))
}
}
response
} catch (error: IOException) {
throw ServiceException(error)
}
}
而且我正在正确地排队呼叫,我只是发送一个 503 作为响应......
这是我的测试
@Test(expected = MyException.ServerUnavailable::class)
fun test2() {
mock Response with HttpURLConnection.HTTP_UNAVAILABLE
runBlocking {
apiService.doThecall()
}
}
我在那里想念什么?