0

currently I'm working on an android application. The data used by this app is provided by a web server. Now I want to test the app and play a bit with it using test data, cause the real data from the server changes dynamically and I'm not always able to test particular functions or to look at particular things. Two options which come in my mind are:

  • Store test data in a local database / file. Add a global variable like boolean useTestData and change the web server connectors such they don't contact the web server in case useTestData is set to true. Disadvantage is, that I have to modify all connectors with code, which won't be used in the released app.

  • Implement second web server API for testing purpose, which will provide only static test data that I need. In case the global variable is set to true, the test API will be accessed.

I think the second way is the better choice. Maybe there is some other alternative way to do so? What do you think about this?

Thanks in advance.

1 Answer 1

0

The best way I see here is to make dependency injection. What is wrong with your suggestions(briefly):

  1. You will break logic of your connectors. Connectors mustn't depend on your tests.

  2. You're thinking in the right direction, but what would you do if you lose connection or your server break down. This approach makes your tests a bit flaky. Results of the test will depend on state of server and network connection of computer where tests are running and maybe on something else. That is wrong. If your code is written well tests must be succeeded always, I mean regardless of anything else code.

So I suggest to abstract from your connector.

public interface IConnector {
    // e.g.
    public JSONObject getData(String what);
}

public class Connector implements IConnector {
    public JSONObject getData(String what) {
        // actual implementation that is already written
        // here you make requests to server and so forth
    }
}

// mock implementation of your api
public class TestConnector implements IConnector {
    public JSONObject getData(String what) {
        // predictable behaviour without requests to server
    }
}

public class MyActivity extends Activity {
    public IConnector connector = new Connector();

    /**
     * This method substitutes implementation of connector.
     * Use this for testing purposes.
     */
    public void setConnector(IConnector impl) {
        connector = impl;
    }

    //rest of the code...
}

Then you will get predictable and determenistic behaviour in the tests without massive changes in the code.

Btw I heard about Dagger. That is library for dependency injection that should help in such situations. But I didn't have time to try it so can't say definite information about it.

Hopefully, this would help you.

1
  • Thank you for this suggestion, I think it is the way I was looking for. Gonna try it out!
    – valdes21
    Jul 19, 2015 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.