I'm trying to test our Android app using MockWebService. The request send to server is received correctly, however, the response didn't back to the client. This only happen when doing request asynchronously, when using httpclient directly it works fine...
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);
}
}
The code we doing the test is also very simple:
@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());
}
Any idea or help appreciate! Thanks!
Aucun commentaire:
Enregistrer un commentaire