0

I have an activity that rely heavily on data that is fetched from a RESTful server. In order to test , currently I get around the http requests and hand over the json object to the activity - manually.

However, this approach is getting more more and more cumbersome and I was wondering - is there any good strategy in testing activities that rely on server-client communication?

4

2 回答 2

1

You can create a mock-server. You can do that at two levels:

  1. Nicely package your server-access code on the client, so you can replace it with a mock-server-access code that doesn't contact anything, just returns the result, then test the client version with the mock-server-access.

  2. Implement an actual mock-server that returns the data, and point the client to it.

I think the second option is better, as it will allow you to test the client app under all sorts of conditions - including no communications.

于 2013-10-01T08:11:44.640 回答
1

You should build your activity that will be easily work with integrations tests. For this you have to build your modules separately with no dependencies between them.

For example, if you have Activity that relay on some data that fetched from the server, your components should look something like this:

public class AppActivity extends Activity {
    private DataFetcher fetcher;

    public AppActivity() {
        //use this for real
        fetcher = new ServerDataFetcher();
        //or use this for tests
        fetcher = new MockDataFetcher();
        //you can even select it using DI frameworks 
    }
}

public interface DataFetcher {
    public Data fetchData();
}

public class ServerDataFetcher implement DataFetcher{

    public Data fetchData() {
        //get your data from server
    }
}

public class MockDataFetcher implement DataFetcher{

    public Data fetchData() {
        //return a pre-fetched data
    }
}
于 2013-10-01T08:19:07.497 回答