MockWebServer的takeRequest()方法需要很长时间才能响应或挂起
2022-09-04 23:04:31
我在我的Android JUnit测试中使用MockWebServer库。我正在测试一个调用服务器的SDK。因此,我使用覆盖这些服务器URL并捕获SDK发送的内容以对其进行断言。MockWebServer
我遇到的问题是,如果我尝试将其分配给一个新变量,测试会在第二个变量上挂起,有时甚至在第一个变量上挂起 - 如果我在模拟器上运行它,它会在第一个方法上挂起,但是如果我在我的物理Android设备上运行它, 它冻结在第二种方法上。server.takeRequest()
RecordedRequest
server.takeRequest()
server.takeRequest()
server.takeRequest()
public void testSomething() {
final MockWebServer server = new MockWebServer();
try {
server.play();
server.enqueue(new MockResponse().setBody("")
.setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR));
server.enqueue(new MockResponse().setBody("")
.setResponseCode(HttpURLConnection.HTTP_OK));
server.enqueue(new MockResponse().setBody("")
.setResponseCode(HttpURLConnection.HTTP_OK));
URL url = server.getUrl("/");
// This internal method overrides some of the hardcoded URLs
// within the SDK that I'm testing.
Util.overrideUrls(url.toString())
// Do some server calls via the SDK utilizing the mock server url.
RecordedRequest requestFor500 = server.takeRequest();
// Do some assertions with 'requestFor500'
// Do some more server calls via the SDK utilizing the mock server url.
/*
* This is the part where the JUnit test hangs or seems to go into
* an infinite loop and never recovers
*/
RecordedRequest requestAfter500Before200 = server.takeRequest();
} catch {
...
}
}
是我做错了什么,还是这是某种类型的错误?MockWebServer