ライブラリ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));
}
});
}
テストクラスのHTTPクライアント:
@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);
}
あなたはそれを嘲笑しようとしましたか? – abbath
どのようにonSuccessとonFailureをテストしたいですか –
この問題の解決策を見つけました – Ezio