简体   繁体   中英

Mocking responses in Robospice

Is it any possibility to mock responses using Robospice in Android because I need to create demo application which will have all responses in json files in application? I was trying to use https://github.com/andrzejchm/RESTMock but there are some problems, I'm getting information: RESTMock successfully started! url: http://localhost:34163/ RESTMock successfully started! url: http://localhost:34163/ and nothing else. Any other idea?

Within your Application class you can start RESTMock with:

@Override
public void onCreate() {
    super.onCreate();
    RESTMockServerStarter.startSync(new AndroidAssetsFileParser(getContext()),new AndroidLogger());

    RESTMockServer.whenRequested(pathContains("response"))
        .thenReturnFile(200, "response.json");
}

This does set up your RESTMock server, but does not do any http requests as of yet. You now need to provide your RESTMock's server url to the methods that fetch data. For example, if you're using Retrofit:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RESTMockServer.getUrl())
            .build();

From now on, each request made using retrofit should communicate with the RESTMock server directly and properly fetch responses that you specify as mocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM