2

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);
        }
}
Jake Rushing
  • 99
  • 10

1 Answers1

2

There are two problems with your test.

The first issue is:

@Before
public void setup() {
    FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
}

You want the FakeHttpLayer to return fake responses, but then you tell it not to intercept requests. This can be removed entirely because it defaults to true.

The second issue is:

assertEquals("Awwwwww yeeeeaaaaahhhh", mHttpResponseMock);

At this point, you will get the correct fake http response, but you are asserting that a String Object is equal to an HttpResponse response object, which is incorrect.

You need to read the response body from an InputStream provided by the HttpResponse. See: How can I get an http response body as a string in Java?

Luckily, that can be as simple as:

assertEquals("Awwwwww yeeeeaaaaahhhh", EntityUtils.toString(mHttpResponseMock.getEntity(), "UTF-8"));

Now your test should pass!

Community
  • 1
  • 1
Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21