如何对 http 请求进行单元测试 android
how to unit test http request android
我使用了库 Robolectric
另一个可能的框架。 Android http://loopj.com/android-async-http/
的 HTTP 客户端
static AsyncHttpClient client = new AsyncHttpClient();
public static void getData (final ServerCallback callback) {
client.get("http://httpbin.org/get", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(statusCode, new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.onFailure(statusCode, new String(responseBody));
}
});
}
测试Class:
@RunWith(RobolectricTestRunner.class)
public class ApiTest{
@Test
public void testgetData () {
}
}
界面
public interface ServerCallback {
// Api detect connection
void onSuccess(int statusCode, String response);
void onFailure(int statusCode, String response);
}
Роман,
我将提供一个一般性的答案,以帮助您朝着正确的方向前进。如果您对它们的使用有疑问,我首选的测试 http 请求的方法是使用 square okhttp mockwebserver. Review the project's unit tests。
在您的特定情况下,有几件事需要解决:
- 您需要覆盖测试中使用的基础url
- android-async-http 库是异步的,但是,为了 运行 一致的单元测试,您需要 request/response 同步
因此,按照您的示例,让我们按如下方式设置您的测试客户端:
public class TestHttpClient {
// package-local client that can be set in tests
static AsyncHttpClient client = new AsyncHttpClient();
// package-local baseUrl that can be set in tests
static String baseUrl = "http://pastbin.org/";
public static void getData(final ServerCallback callback) {
String url = baseUrl + "get";
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(statusCode, new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.onFailure(statusCode, new String(responseBody));
}
});
}
}
要测试 TestHttpClient
,请使用 MockWebServer
启动服务器进行测试,并设置客户端向服务器发出请求:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = Build.VERSION_CODES.LOLLIPOP)
public class TestHttpRequestTest {
@Rule
public MockWebServer mockWebServer = new MockWebServer();
@Test
public void getData_onSuccess_doesSomething() throws InterruptedException {
// Here we are creating a mock ServerCallback. We will use
// this mock object to verify the callback is invoked
ServerCallback callback = mock(ServerCallback.class);
// To test the client, we need to send a request to the mock mockWebServer.
// The MockWebServer spins up an actual backend to handle calls. You MUST
// setup the client to use the base Url of the mockWebServer instead of
// the actual url e.g.: http://httpbin.org.
TestHttpClient.baseUrl = mockWebServer.url("/").toString();
// For testing, use a synchronous client so that we can get the response before
// the test completes.
TestHttpClient.client = new SyncHttpClient();
// Set up the mockWebServer to return a MockResponse with
// some data in the body. This data can be whatever you want... json, xml, etc.
// The default response code is 200.
mockWebServer.enqueue(new MockResponse().setBody("success"));
// To simulate an error
// mockWebServer.enqueue(new MockResponse().setResponseCode(500).setBody("error"));
TestHttpClient.getData(callback); // calling the method under test
verify(callback).onSuccess(200, "success"); // using verify of mockito
}
}
备注:
async http client
库需要一些 android 系统组件才能正常工作,因此您必须使用 @RunWith(...)
注释
MockWebServer
需要将 sdk 设置为 v21 或更高版本的 @Config(sdk = 21)
才能与 Robolectric. 一起使用
要在您的项目中包含 mockwebserver 和 mockito,请将以下内容添加到 build.gradle
dependencies {
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
testCompile 'org.mockito:mockito-core:1.10.19'
}
测试愉快!!!
我使用了库 Robolectric
另一个可能的框架。 Android http://loopj.com/android-async-http/
static AsyncHttpClient client = new AsyncHttpClient();
public static void getData (final ServerCallback callback) {
client.get("http://httpbin.org/get", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(statusCode, new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.onFailure(statusCode, new String(responseBody));
}
});
}
测试Class:
@RunWith(RobolectricTestRunner.class)
public class ApiTest{
@Test
public void testgetData () {
}
}
界面
public interface ServerCallback {
// Api detect connection
void onSuccess(int statusCode, String response);
void onFailure(int statusCode, String response);
}
Роман,
我将提供一个一般性的答案,以帮助您朝着正确的方向前进。如果您对它们的使用有疑问,我首选的测试 http 请求的方法是使用 square okhttp mockwebserver. Review the project's unit tests。
在您的特定情况下,有几件事需要解决:
- 您需要覆盖测试中使用的基础url
- android-async-http 库是异步的,但是,为了 运行 一致的单元测试,您需要 request/response 同步
因此,按照您的示例,让我们按如下方式设置您的测试客户端:
public class TestHttpClient {
// package-local client that can be set in tests
static AsyncHttpClient client = new AsyncHttpClient();
// package-local baseUrl that can be set in tests
static String baseUrl = "http://pastbin.org/";
public static void getData(final ServerCallback callback) {
String url = baseUrl + "get";
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(statusCode, new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.onFailure(statusCode, new String(responseBody));
}
});
}
}
要测试 TestHttpClient
,请使用 MockWebServer
启动服务器进行测试,并设置客户端向服务器发出请求:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = Build.VERSION_CODES.LOLLIPOP)
public class TestHttpRequestTest {
@Rule
public MockWebServer mockWebServer = new MockWebServer();
@Test
public void getData_onSuccess_doesSomething() throws InterruptedException {
// Here we are creating a mock ServerCallback. We will use
// this mock object to verify the callback is invoked
ServerCallback callback = mock(ServerCallback.class);
// To test the client, we need to send a request to the mock mockWebServer.
// The MockWebServer spins up an actual backend to handle calls. You MUST
// setup the client to use the base Url of the mockWebServer instead of
// the actual url e.g.: http://httpbin.org.
TestHttpClient.baseUrl = mockWebServer.url("/").toString();
// For testing, use a synchronous client so that we can get the response before
// the test completes.
TestHttpClient.client = new SyncHttpClient();
// Set up the mockWebServer to return a MockResponse with
// some data in the body. This data can be whatever you want... json, xml, etc.
// The default response code is 200.
mockWebServer.enqueue(new MockResponse().setBody("success"));
// To simulate an error
// mockWebServer.enqueue(new MockResponse().setResponseCode(500).setBody("error"));
TestHttpClient.getData(callback); // calling the method under test
verify(callback).onSuccess(200, "success"); // using verify of mockito
}
}
备注:
async http client
库需要一些 android 系统组件才能正常工作,因此您必须使用@RunWith(...)
注释MockWebServer
需要将 sdk 设置为 v21 或更高版本的@Config(sdk = 21)
才能与 Robolectric. 一起使用
要在您的项目中包含 mockwebserver 和 mockito,请将以下内容添加到 build.gradle
dependencies {
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
testCompile 'org.mockito:mockito-core:1.10.19'
}
测试愉快!!!