我的模拟服务器调度程序永远不会在模拟器运行 API 28 及更高版本上达到覆盖方法,但它在其他版本上运行良好。知道如何触发它吗?还是只是 API 版本问题?
我指的是本地主机:8080。并且okhttp版本是4.2.1。
fun search() {
sleepSafely(3000)
mockServer = MockWebServer()
mockServer.dispatcher = ErrorDispatcher()
mockServer.start(8080)
sleepSafely(3000)
// do the API request
}
public class ErrorDispatcher extends Dispatcher {
@NotNull
@Override
public MockResponse dispatch(RecordedRequest request) {
// never be triggered
String path = request.getPath();
if (path.equalsIgnoreCase("/api/v2/search/person")) {
return new MockResponse()
.setResponseCode(404)
.setBody("{"MOCK_KEY": "MOCK_VALUE"}");
} else if (path.equalsIgnoreCase("/api/v2/search/book")) {
return new MockResponse()
.setResponseCode(404);
} else {
return new MockResponse().setResponseCode(404);
}
}
}