Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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
Java 在Robolectric 3.1.2和x27中设置FakeHttp.setDefaultHttpResponse;Don’别给我答复_Java_Android_Unit Testing_Http_Robolectric - Fatal编程技术网

Java 在Robolectric 3.1.2和x27中设置FakeHttp.setDefaultHttpResponse;Don’别给我答复

Java 在Robolectric 3.1.2和x27中设置FakeHttp.setDefaultHttpResponse;Don’别给我答复,java,android,unit-testing,http,robolectric,Java,Android,Unit Testing,Http,Robolectric,我正在使用最新发布的Robolectric版本(3.1.2) 我正在为一个依赖于API的应用程序编写测试,所以我确实需要确保API正常工作 我能够进行不需要模拟响应的http请求调用。但是,当我确实需要调用模拟响应时,我就遇到了如何获得模拟响应的问题 当我试图模拟响应时,我总是得到默认的http响应,而不是我想要得到的字符串响应 我一直在谷歌上搜索如何模拟http响应,我尝试了尽可能多的建议,但我无法让它发挥作用 任何建议都非常感谢,因为我在这里抓住了救命稻草 这是我正在使用的gradle和测试

我正在使用最新发布的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);
}
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
您希望FakeHttpLayer返回假响应,但随后告诉它不要拦截请求。这可以完全删除,因为它默认为true

第二个问题是:

@Before
public void setup() {
    FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}
assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);
此时,您将获得正确的伪http响应,但您断言
String
对象等于
HttpResponse
响应对象,这是不正确的

您需要从
HttpResponse
提供的
InputStream
中读取响应正文。见:

幸运的是,这可以简单到:

assertEquals("Awwwwww yeeeeaaaaahhhh", EntityUtils.toString(mHttpResponseMock.getEntity(), "UTF-8"));
现在你的考试应该通过了