是否可以使用 robolectric 进行真正的 http 请求 [英] Is it possible to do real http requests with robolectric

查看:44
本文介绍了是否可以使用 robolectric 进行真正的 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 robolectric 的问题是它不能产生真正的 HttpRequests.

The issue I have with robolectric is that it doesn't make real HttpRequests.

我可以使用真正调用本地测试服务器的真实实现来替换 robolectric 的 HttpClient 实现吗?

Could I replace the HttpClient implementation of robolectric using a real one actually making the calls to my local test server?

推荐答案

Jan Berkel 提交了一个拉取请求 到 3 月 8 日合并为 master 的 Robolectric.您现在可以在测试开始时调用 Robolectric.getFakeHttpLayer().interceptHttpRequests(false); 以绕过 Robolectric 中的模拟 http 层.

Jan Berkel submitted a pull request to Robolectric that was merged into master on March 8th. You can now call Robolectric.getFakeHttpLayer().interceptHttpRequests(false); in the beginning of your test to bypass the mocked http layer in Robolectric.

您可以直接从 Sonatype 下载 jar 以包含在您的 Eclipse Android 项目中.如果您使用的是 Maven 并且 1.1 尚未发布(检查上面的 Sonatype 网址),那么您需要添加 sonatype 快照存储库并使用 robolectric 1.1-SNAPSHOT.您可以通过将以下内容添加到 pom.xml

You can download the jar directly from Sonatype to include in your Eclipse Android project. If you are using Maven and 1.1 has not yet been released (check at the Sonatype url above) then you will need to add the sonatype snapshot repo and use robolectric 1.1-SNAPSHOT. You can do this by adding the following to your pom.xml

<repositories>
    <repository>
        <id>sonatype-snapshots</id>
        <name>Sonatype Snapshots</name>
        <layout>default</layout>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
...
<dependencies>
   <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
    ...
</dependencies>

我将其用于针对真实服务器运行的集成测试,并且运行良好.

I am using this for integration test that run against a real server and it works well.

这篇关于是否可以使用 robolectric 进行真正的 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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