12

我正在使用OkHttp MockWebServer来模拟我的服务器响应以进行单元测试。

它适用于第一次测试,但在第二次测试中,我的客户失败了:

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

即使第二次测试与第一次完全相同,也会发生这种情况。
这就是我正在做的事情:

@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;
        }
    }
}

第一个测试按原样通过,但第二个测试失败并显示我上面写的消息。
控制台中的输出是:

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

知道为什么会这样吗?

4

2 回答 2

10

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

于 2016-06-22T11:31:25.953 回答
-3

此本地主机连接失败,因为它不属于模拟服务器。改用这个 (mockHelper.getHost()).willReturn(okhttpMockWebServer.url("/").toString())

于 2018-09-11T13:03:55.840 回答