模拟的Htt presponse与Robolectric [英] Mock HttpResponse with Robolectric

查看:332
本文介绍了模拟的Htt presponse与Robolectric的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Robolectric 2.3-SNAPSHOT,我想测试会在后台执行一个请求的对象。为了孤立它,我试图嘲弄的Htt presponse回来了,没有成功后几个小时投入。

Using Robolectric 2.3-SNAPSHOT, I want to test an object that'll execute a request in the background. In order to isolate it, I'm trying to mock the HttpResponse returned, without success after some hours invested.

我已经创建了一个任何人都可以克隆项目。 Simly运行的 ./ gradlew检查的<一个href="https://github.com/Maragues/RobolectricDummyProject">https://github.com/Maragues/RobolectricDummyProject (GIT克隆<一href="https://github.com/Maragues/RobolectricDummyProject.git">https://github.com/Maragues/RobolectricDummyProject.git)

I've created a project that anyone can clone. Simly run ./gradlew check https://github.com/Maragues/RobolectricDummyProject (git clone https://github.com/Maragues/RobolectricDummyProject.git)

我试过

  • Robolectric.setDefaultHtt presponse(200,my_mocked_word);

  • Robolectric.setDefaultHttpResponse(200, "my_mocked_word");

MockWebServer(<一href="https://$c$c.google.com/p/mockwebserver/">https://$c$c.google.com/p/mockwebserver/)

MockWebServer (https://code.google.com/p/mockwebserver/)

但测试失败,因为他们查询真实的URL

But the tests fail because they query the real URL

  private static final String MOCKED_WORD = "MOCKED";

  @Test
  public void mockedRequestUsingMockServer() throws Exception {
    mMockWebServer.enqueue(new MockResponse().setBody(MOCKED_WORD));
    mMockWebServer.play();

    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    Robolectric.getFakeHttpLayer().interceptResponseContent(false);

    String result = request.loadDataFromNetwork();

    assertEquals(MOCKED_WORD, result);
  }

  @Test
  public void mockedRequestUsingRobolectric() throws Exception {
    Robolectric.setDefaultHttpResponse(200, MOCKED_WORD);

    String result = request.loadDataFromNetwork();

    assertEquals(MOCKED_WORD, result);
  }

在code执行请求

The code executing the request

public String loadDataFromNetwork() throws Exception {
    // With Uri.Builder class we can build our url is a safe manner
    Uri.Builder uriBuilder = Uri.parse("http://robospice-sample.appspot.com/reverse").buildUpon();
    uriBuilder.appendQueryParameter("word", word);

    String url = uriBuilder.build().toString();

    HttpURLConnection urlConnection = (HttpURLConnection) new URL(url)
      .openConnection();
    String result = IOUtils.toString(urlConnection.getInputStream());
    urlConnection.disconnect();

    return result;
  }

也许相关的问题

Possibly related questions

  • 无法捕捉robolectric HTTP请求(我试过如果没有成功。也许我失去了一些东西)
  • <一个href="http://stackoverflow.com/questions/4998176/anyone-had-success-mocking-htt$p$pquests-with-robolectric">Anyone过成功嘲讽的Htt prequests与Robolectric?(我没有使用eclipse)
  • Can't capture HTTP request with robolectric (I've tried that without success. Perhaps I'm missing something)
  • Anyone had success mocking HttpRequests with Robolectric? (I'm not using eclipse)

推荐答案

原来,Robolectric的<一个href="https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/tester/org/apache/http/FakeHttpLayer.java"相对=nofollow> FakeHttpLayer 只适用于Apache的HttpClient的,这是非常鼓励的版本高于升级Froyo。从 Robolectric的谷歌集团

It turns out that Robolectric's FakeHttpLayer only works with Apache's HttpClient, which is highly discouraged on versions greater than Froyo. Extracted from Robolectric's Google Group

话虽这么说,HttpURLConnection的的使用会导致你的麻烦。我想尝试使用Android的HttpClient的执行在可能的,因为Robolectric拦截所有调用的库,并允许您设置罐头回应你的HTTP调用。我们正在做同样的HttpURLConnection的,虽然目前还不清楚时会发生的。

除此之外,一个单元测试不应需要模拟HTTP层。我的做法是错误的,从一开始。

Apart from that, a unit test should not need to mock the HTTP layer. My approach was wrong from the beginning.

这篇关于模拟的Htt presponse与Robolectric的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆