Android http testing with Robolectric

安稳与你 提交于 2020-01-12 04:50:07

问题


I have an Android app where the main part of the app is the APIcalls.java class where I make http requests to get data from server an display the data in the app.

I wanted to create unit test for this Java class since it's the most part of the app. Here is the method for getting the data from server:

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;

I thought I can make simple API calls from the tests like this:

api.get("www.example.com")

But every time I make some http calls from the tests, I get an error:

Unexpected HTTP call GET

I know I am doing something wrong here, but can anyone tell me how can I properly test this class in Android?


回答1:


Robolectric provides some helper methods to mock http response for DefaultHttpClient. If you use DefaultHttpClient without using those methods, you would get a warning message.

Here is an example of how to mock http response:

@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"));
    }
}

You can find more examples by looking at Robolectric's test codes.




回答2:


Thank you for all your answers but I found what I was looking for. I wanted to test real HTTP calls.

By adding Robolectric.getFakeHttpLayer().interceptHttpRequests(false); you tell Robolectric not to intercept these requests and it allows you to make real HTTP calls




回答3:


I answered another version of this same question, but...

What you have here is not using anything from Android, so Robolectric is basically irrelevant. This is all standard Java and the Apache HTTP library. You simply need a mocking framework and dependency injection to simulate the HttpClient (see my other answer for links). It doesn't have network access while unit testing, and so it fails.

When testing classes that use parts of the Android framework, you can use Robolectric (or similar) to mock or simulate Android.jar since your unit testing framework isn't going to have access to that either.



来源:https://stackoverflow.com/questions/18816395/android-http-testing-with-robolectric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!