vendredi 2 septembre 2016

Set up FakeHttp.setDefaultHttpResponse in Robolectric 3.1.2 doesn't give me the response

I'm using the latest released version for Robolectric (3.1.2).

I was writing test for an app that's depends on the API, so I do need to make sure the api works.

I was able to make a http request calls that don't require the mock response. But when I do need to make calls for a mock response, this is where I have issues how to get the mock response.

When I try to mock a response, I always get the default http response instead of the string response that I want to get.

I have been googling how to mock the http responses, and I've tried out as many suggestions as I can, and which I can't get it to work.

Any suggestions are greatly appreciated, as I'm grasping at the straws here.

Here is the gradle and test case I'm working with

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);
        }
}

Aucun commentaire:

Enregistrer un commentaire