在 Robolectric 中设置 FakeHttp.setDefaultHttpResponse 3.1.2 没有给我响应
Set up FakeHttp.setDefaultHttpResponse in Robolectric 3.1.2 doesn't give me the response
我使用的是 Robolectric (3.1.2) 的最新发布版本。
我正在为依赖于 API 的应用程序编写测试,因此我确实需要确保 api 正常工作。
我能够发出不需要模拟响应的 http 请求调用。但是当我确实需要调用模拟响应时,这就是我如何获得模拟响应的问题。
当我尝试模拟响应时,我总是得到默认的 http 响应,而不是我想要得到的字符串响应。
我一直在谷歌搜索如何模拟 http 响应,我已经尝试了尽可能多的建议,但我无法让它工作。
非常感谢任何建议,因为我正在抓住这里的救命稻草。
这是我正在使用的 gradle 和测试用例
build.gradle
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.1.2'
testCompile 'org.robolectric:shadows-multidex:3.1.2'
testCompile 'org.robolectric:shadows-httpclient:3.1.2'
Test case
@Before
public void setup() {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}
@Test
public void VerifyAwwYeeaaahhhTest() {
mainActivity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
shadowMain = Shadows.shadowOf(mainActivity);
FakeHttp.getFakeHttpLayer().interceptResponseContent(false);
String url = "https://example.com";
FakeHttp.setDefaultHttpResponse(200, "Awwwwww yeeeeaaaaahhhh");
try {
DefaultHttpClient client = new DefaultHttpClient();
mHttpGetMock = new HttpGet(url);
mHttpGetMock.addHeader("Authroization", "12345imatoken67890");
mHttpGetMock.addHeader("Accept-Encoding", "G-Zip");
mHttpResponseMock = client.execute(mHttpGetMock);
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
System.out.println("Successful Aww yeeaah Test");
} catch (UnsupportedEncodingException e) {
System.out.println("Unsuccessful Aww yeeaah Test. Unsupported Encoding Exception thrown");
System.out.println(e);
} catch (ClientProtocolException e) {
System.out.println("Unsuccessful Aww yeeaah Test. Client Protocol Exception thrown");
System.out.println(e);
} catch (IOException e) {
System.out.println("Unsuccessful Aww yeeaah Test. IO Exception thrown");
System.out.println(e);
}
}
你的测试有两个问题。
第一期是:
@Before
public void setup() {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}
你想要 FakeHttpLayer
到 return 假响应,但你告诉它不要拦截请求。这可以完全删除,因为它默认为 true。
第二期是:
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
此时,您将获得正确的假 http 响应,但您断言 String
对象等于 HttpResponse
响应对象,这是不正确的。
您需要从 HttpResponse
提供的 InputStream
中读取响应正文。参见:
幸运的是,这可以很简单:
assertEquals("Awwwwww yeeeeaaaaahhhh", EntityUtils.toString(mHttpResponseMock.getEntity(), "UTF-8"));
现在你的测试应该通过了!
我使用的是 Robolectric (3.1.2) 的最新发布版本。
我正在为依赖于 API 的应用程序编写测试,因此我确实需要确保 api 正常工作。
我能够发出不需要模拟响应的 http 请求调用。但是当我确实需要调用模拟响应时,这就是我如何获得模拟响应的问题。
当我尝试模拟响应时,我总是得到默认的 http 响应,而不是我想要得到的字符串响应。
我一直在谷歌搜索如何模拟 http 响应,我已经尝试了尽可能多的建议,但我无法让它工作。
非常感谢任何建议,因为我正在抓住这里的救命稻草。
这是我正在使用的 gradle 和测试用例
build.gradle
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.1.2'
testCompile 'org.robolectric:shadows-multidex:3.1.2'
testCompile 'org.robolectric:shadows-httpclient:3.1.2'
Test case
@Before
public void setup() {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}
@Test
public void VerifyAwwYeeaaahhhTest() {
mainActivity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
shadowMain = Shadows.shadowOf(mainActivity);
FakeHttp.getFakeHttpLayer().interceptResponseContent(false);
String url = "https://example.com";
FakeHttp.setDefaultHttpResponse(200, "Awwwwww yeeeeaaaaahhhh");
try {
DefaultHttpClient client = new DefaultHttpClient();
mHttpGetMock = new HttpGet(url);
mHttpGetMock.addHeader("Authroization", "12345imatoken67890");
mHttpGetMock.addHeader("Accept-Encoding", "G-Zip");
mHttpResponseMock = client.execute(mHttpGetMock);
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
System.out.println("Successful Aww yeeaah Test");
} catch (UnsupportedEncodingException e) {
System.out.println("Unsuccessful Aww yeeaah Test. Unsupported Encoding Exception thrown");
System.out.println(e);
} catch (ClientProtocolException e) {
System.out.println("Unsuccessful Aww yeeaah Test. Client Protocol Exception thrown");
System.out.println(e);
} catch (IOException e) {
System.out.println("Unsuccessful Aww yeeaah Test. IO Exception thrown");
System.out.println(e);
}
}
你的测试有两个问题。
第一期是:
@Before
public void setup() {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}
你想要 FakeHttpLayer
到 return 假响应,但你告诉它不要拦截请求。这可以完全删除,因为它默认为 true。
第二期是:
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
此时,您将获得正确的假 http 响应,但您断言 String
对象等于 HttpResponse
响应对象,这是不正确的。
您需要从 HttpResponse
提供的 InputStream
中读取响应正文。参见:
幸运的是,这可以很简单:
assertEquals("Awwwwww yeeeeaaaaahhhh", EntityUtils.toString(mHttpResponseMock.getEntity(), "UTF-8"));
现在你的测试应该通过了!