OkHttp MockWebServer重新启动时无法接受连接

编程入门 行业动态 更新时间:2024-10-25 12:25:33
本文介绍了OkHttp MockWebServer重新启动时无法接受连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 OkHttp MockWebServer 来模拟我对单元的服务器响应测试.

I'm using the OkHttp MockWebServer to mock my server responses for unit tests.

在第一次测试中效果很好,但是在第二次测试中,我的客户失败了:

It works great for the first test, but on the 2nd test my client fails with:

无法连接到localhost/0:0:0:0:0:0:0:0:1:63631

Failed to connect to localhost/0:0:0:0:0:0:0:1:63631

即使第二次测试与第一次测试完全相同,也会发生这种情况. 这是我在做什么:

This happens even if the 2nd test is exactly the same as the 1st one. Here's what I'm doing:

@RunWith(RobolectricTestRunner.class) @Config(shadows = MyClassTest.MyNetworkSecurityPolicy.class, manifest = "src/main/AndroidManifest.xml", constants = BuildConfig.class, sdk = 16) public class MyClassTest { private MockWebServer mockServer; private MyServerApi serverApi; @Before public void setUp() throws Exception { System.out.println("\ntest start"); this.mockServer = new MockWebServer(); this.mockServer.start(); this.serverApi = new MyServerApi(this.mockServer.url("/").toString()); } @Test public void testOne() throws Exception { final String responseBody = // read response from file this.mockServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseBody)); final Waiter waiter = new Waiter(); this.serverApi.getData("some_id", new Callback<MyResponseData> { @Override public void onResponse(final Call<MyResponseData> call, final Response<MyResponseData> response) { waiter.assertEquals("some_value", response.body().getValue()); waiter.resume(); } @Override public void onFailure(final Call<T> call, final Throwable error) { waiter.fail(error); } }); waiter.await(); final RecordedRequest recordedRequest = this.mockServer.takeRequest(); assertEquals("GET", recordedRequest.getMethod()); } @Test public void testTwo() throws Exception { final String responseBody = // read response from file this.mockServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseBody)); final Waiter waiter = new Waiter(); this.serverApi.getData("some_id", new Callback<MyResponseData> { @Override public void onResponse(final Call<MyResponseData> call, final Response<MyResponseData> response) { waiter.assertEquals("some_value", response.body().getValue()); waiter.resume(); } @Override public void onFailure(final Call<T> call, final Throwable error) { waiter.fail(error); } }); waiter.await(); final RecordedRequest recordedRequest = this.mockServer.takeRequest(); assertEquals("GET", recordedRequest.getMethod()); } @After public void tearDown() throws Exception { System.out.println("test end\n"); this.mockServer.shutdown(); } @Implements(NetworkSecurityPolicy.class) public static class MyNetworkSecurityPolicy { @Implementation public static NetworkSecurityPolicy getInstance() { try { Class<?> shadow = MyNetworkSecurityPolicy.class.forName("android.security.NetworkSecurityPolicy"); return (NetworkSecurityPolicy) shadow.newInstance(); } catch (Exception e) { throw new AssertionError(); } } @Implementation public boolean isCleartextTrafficPermitted() { return true; } } }

第一个测试应该通过,但是第二个失败,并显示了我上面写的消息. 控制台中的输出为:

The first test passes as it should, but the second one fails with the message I wrote above. The output in the console is:

test start okhttp3.mockwebserver.MockWebServer$3 execute INFO: MockWebServer[63631] starting to accept connections WARNING: no system properties value for gsm.sim.operator.alpha okhttp3.mockwebserver.MockWebServer$4 processOneRequest INFO: MockWebServer[63631] received request: GET REQUEST_PATH HTTP/1.1 and responded: HTTP/1.1 200 OK okhttp3.mockwebserver.MockWebServer$3 acceptConnections test end INFO: MockWebServer[63631] done accepting connections: Socket closed test start okhttp3.mockwebserver.MockWebServer$3 execute INFO: MockWebServer[63649] starting to accept connections okhttp3.mockwebserver.MockWebServer$3 acceptConnections INFO: MockWebServer[63649] done accepting connections: Socket closed on error: Failed to connect to localhost/0:0:0:0:0:0:0:1:63631 test end

(Waiter对象来自 concurrentunit lib )

知道为什么会这样吗?

推荐答案

您的第二个请求正在使用第一个MockWebServer实例的URL. (每个实例都有一个不同的URL.)

Your second request is using the URL of the first MockWebServer instance. (Each instance has a distinct URL.)

更多推荐

OkHttp MockWebServer重新启动时无法接受连接

本文发布于:2023-10-28 17:54:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   OkHttp   MockWebServer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!