Is it possible to do real http requests with robol

2019-01-17 23:23发布

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

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

2条回答
来,给爷笑一个
2楼-- · 2019-01-17 23:29

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.

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.

查看更多
来,给爷笑一个
3楼-- · 2019-01-17 23:55

Basically you don't. Robolectric is a test framework and this is how it designed to work, what you asked is not what we do with Robolectric.

I am sure you have came cross this blog taking about using Robolectric test HTTP requests. Actually the title "How to test HTTP requests" is not quite accurate IMO. I would rather call it "How to mock HTTP requests", as stated in this blog, the best place to use Robolectric mock http request is where you don't really want to bother real network calls to remote server maybe during Project development/testing phase.

Suppose I have a main activity connect to my bank account for some operation, the submit button should post my input info to remote server update my account info, then based on different responses bring me to several different activities. I have written all code smoothly, HttpClient, form with submit button, new intent to next activity, etc. all nice and clean. Right, how can I test when click submit button, the proper activity is opened based on those different response, without either need change my Activity source code or use real network call to screw my real bank account. The answer is Robolectric mock http request. The key point of using Robolectric in this case is not for testing real http request, instead, it is used for fooling my httpClient code and test other part of my application code like UI stuff. Ultimately, the real network test should be perform on emulator or a real device at some stage when your deployment come to a final stable state.

Another advantage claimed by Robolectirc team is you don't need start instrumentation test on emulator or device, which is inefficient and slow down application development/test life cycle.

查看更多
登录 后发表回答