0

我正在尝试使用 MockWebService 测试我们的 Android 应用程序。发送到服务器的请求被正确接收,但是响应没有返回给客户端。这只发生在异步请求时,直接使用httpclient时它工作正常......

   protected void signIn(String macAddress) {
   RequestHandle requestHandle = NetTask.signIn(this, object, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            Log.e("TAG","Login failed responseString="+responseString+",statusCode="+statusCode);
            ToastShowHelper.showSourceErrorToast(SignInActivity.this, statusCode, responseString);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            super.onSuccess(statusCode, headers, responseString);
            Log.d("TAG", "Login succeed responseString=" + responseString + ",statusCode=" + statusCode);
            onSignInSuccess(responseString);
        }
}

我们做测试的代码也很简单:

@Test
public void TestSignIn() throws URISyntaxException, InterruptedException {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8").setBody(requestBody));
    try {
        server.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    baseUrl = server.url(testUrl);
    UrlUtil.setTestLeftUrl(mockHttpRequest.getLeftUrl());
    email.setText("plapla@testemail.com");
    password.setText("123456");
    signInActivity.signIn("mac_addr");
    assertEquals("{\"mac_address\":\"mac_addr\",\"email\":\"plapla@testemail.com\",\"from\":\"ANDROID\",\"password\":\"123456\",\"has_mac\":true}", mockHttpRequest.isTestUrl().getBody().readUtf8());

}

任何想法或帮助感谢!谢谢!

4

1 回答 1

0

我想你只需要等待你的异步调用,所以也许使用

Thread.sleep(3000) // 3 secs

在断言之前。

于 2017-03-21T17:01:39.573 回答