greut
greut

Reputation: 4363

How to properly mock HttpGet call on Android test

I've got an application that performs HTTP GET calls using HttpGet and I would like to mock the response in order to test different scenarios without having to setup any specific local server that would act like the remote one.

The goal is to have very high level tests that acts like a real user (Robotium) and fake the response that the application would obtain calling the real server. Much like testing a Twitter client, if you need an example.

Upvotes: 3

Views: 6446

Answers (4)

user3406366
user3406366

Reputation: 11

Try XML Mimic, that will solve your problem. It is easy to configure and runs as independent server.

http://sourceforge.net/projects/xmlmimic/

Upvotes: 0

Divjot Singh
Divjot Singh

Reputation: 114

Google provides a library named as Mockwebserver which can be used for mocking web service response. https://code.google.com/p/mockwebserver/ You can refer this link

Upvotes: 2

fedepaol
fedepaol

Reputation: 6862

How about using Mockito ?

According to this article its latest version should support dalvik, so you should be able to use it with robotium.

With mockito you can mock any object to return whatever you want. I found it very powerful and concise.

Upvotes: 1

koljaTM
koljaTM

Reputation: 10262

Ok, so this is what I did to get fake HttpResponses in my Robotium tests: - I have a class HttpCallBuilder, that usually just returns a DefaultHttpClient - I added a setHttpClient() method to set a MockHttpClient in my tests (you need to implement (empty) a lot of methods in the HttpClient interface, which I omitted here):

public class MockHttpClient implements HttpClient {

private static Context context;
private final BasicHttpParams params = new BasicHttpParams();

@Override
public HttpResponse execute(HttpUriRequest request) throws IOException,
        ClientProtocolException {
    InputStream mockInputStream = context.getAssets().open(
            MockResponses.forRequest(request));
    return new MockHttpResponse(mockInputStream);
}

@Override
public HttpParams getParams() {
    return params;
}

public static void setContext(Context applicationContext) {
    MockHttpClient.context = applicationContext;
}
}

MockResponses allows you to prime your Mock with the right responses for the situation:

public class MockResponses {

private static final List<String[]> responseMapping = new ArrayList<String[]>();
private static final String BASE = "mocks/";

public static String forRequest(final HttpUriRequest request) {
    final String requestString = request.getURI().toString();
    for (final String[] mapping : responseMapping) {
        if (requestString.matches(mapping[0])) {
            return BASE + mapping[1];
        }
    }
    throw new IllegalArgumentException(
            "No mocked reply configured for request: " + requestString);
}

public static void forRequestDoAnswer(final String regex,
        final String fileToReturn) {
    responseMapping.add(new String[] { regex, fileToReturn });
}

public static void reset() {
    responseMapping.clear();
}
}

In your test you can then prepare your test like this:

HttpCallBuilder.setHttpClient(new MockHttpClient());
MockHttpClient.setContext(context);
MockResponses.reset();
MockResponses.forRequestDoAnswer(".*method=Login.*", "loginform.html");

Upvotes: 3

Related Questions