Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android http测试与Robolectric_Android_Robolectric_Android Testing - Fatal编程技术网

Android http测试与Robolectric

Android http测试与Robolectric,android,robolectric,android-testing,Android,Robolectric,Android Testing,我有一个Android应用程序,其中应用程序的主要部分是APIcalls.java类,我在其中发出http请求以从服务器获取数据,并在应用程序中显示数据 我想为这个Java类创建单元测试,因为它是应用程序的主要部分。以下是从服务器获取数据的方法: StringBuilder sb = new StringBuilder(); try { httpclient = new DefaultHttpClient(); Httpget httpget = new HttpGet(url);

我有一个Android应用程序,其中应用程序的主要部分是APIcalls.java类,我在其中发出http请求以从服务器获取数据,并在应用程序中显示数据

我想为这个Java类创建单元测试,因为它是应用程序的主要部分。以下是从服务器获取数据的方法:

StringBuilder sb = new StringBuilder();

try {

  httpclient = new DefaultHttpClient(); 
  Httpget httpget = new HttpGet(url);

  HttpEntity entity = null;
  try {
    HttpResponse response = httpclient.execute(httpget);
    entity = response.getEntity();
  } catch (Exception e) {
    Log.d("Exception", e);
  }


  if (entity != null) {
    InputStream is = null;
    is = entity.getContent();

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));

      while ((line = reader.readLine()) != null) {
       sb.append(line + "\n");
     }
      reader.close();
    } catch (IOException e) {

           throw e;

       } catch (RuntimeException e) {

           httpget.abort();
           throw e;

       } finally {

         is.close();

       }
       httpclient.getConnectionManager().shutdown();
  }
} catch (Exception e) {
  Log.d("Exception", e);
}

String result = sb.toString().trim();

return result;
我想我可以通过以下测试进行简单的API调用:

api.get("www.example.com")
但每次我从测试中进行一些http调用时,都会出现一个错误:

Unexpected HTTP call GET

我知道我在这里做错了什么,但是有人能告诉我如何在Android中正确测试这个类吗?

我回答了这个问题的另一个版本,但是

这里没有使用Android的任何东西,所以Robolectric基本上是不相关的。这都是标准Java和Apache HTTP库。您只需要一个模拟框架和依赖项注入来模拟HttpClient(请参阅我的其他答案中的链接)。单元测试时,它没有网络访问权限,因此失败


当测试使用Android框架部分的类时,您可以使用Robolectric(或类似工具)来模拟或模拟Android.jar,因为您的单元测试框架也无法访问它。

Robolectric提供了一些帮助方法来模拟DefaultHttpClient的http响应。如果使用DefaultHttpClient而不使用这些方法,则会收到一条警告消息

下面是一个如何模拟http响应的示例:

@RunWith(RobolectricTestRunner.class)
public class ApiTest {

    @Test
    public void test() {
        Api api = new Api();
        Robolectric.addPendingHttpResponse(200, "dummy");
        String responseBody = api.get("www.example.com");
        assertThat(responseBody, is("dummy"));
    }
}

您可以通过查看找到更多示例。

谢谢您的所有答案,但我找到了我要找的内容。 我想测试真正的HTTP调用

通过添加
Robolectric.getFakeHttpLayer().interceptHttpRequests(false)

您告诉Robolectric不要拦截这些请求,它允许您在调用静态函数getFakeHttpLayer()时进行真正的HTTP调用

可能重复的I'm getting编译器错误。看起来它不再是api的一部分,或者我做错了什么。@Gem使用最新版本的Robolectric 3.0,将它添加到build.gradle
testCompile'org.Robolectric:shadows httpclient:3.0'
,然后您可以使用:FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);