brybry
brybry

Reputation: 163

Using MockWebServer 's dispatcher in espresso test to respond to an Async call from another module

I'm writing UI tests for an app that expects a JSON payload delivered as a response to a request made via an Async call which is triggered by a button in the activity I'm testing. I intend to provide various mock payloads to set the state of this activity and assert various items in the UI.

I'm not having any luck with getting the dispatcher to catch the request made via the async call.

The async call is made in a class within a module which is a compile time dependency for my application and I feel like this is where my problem lies, and am not even sure if that is outside the scope of Espresso's abilities.

@Before
public void setup() throws Exception {
    server = new MockWebServer();
    server.start();
    // setting API in my production code to the MockWebServer 
    settingsProvider.setWebServiceURL(server.url("/").toString());

    server.setDispatcher(new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest recordedRequest) throws InterruptedException {

            if (recordedRequest.getPath().startsWith("/v1/customers")) {

                return new MockResponse().setResponseCode(200).setBody(JSONstring);
            }
            return new MockResponse().setResponseCode(204);
        }
    });
    }

If I make a test request from within my actual test, it works:

@Before
    public void testGet(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(server.url("/v1/customers").toString()).build();
        Log.d(LOG_TAG, "Test send GET " + request);

        try {
            Response response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

By adding logs I've verified that my API url is being set correctly and is the same port as the MockWebServer. The app does not not current use any dependency injection.

Main part of async call:

    public void makeAsyncCall(final Application ctx , final AsyncWebServiceRequest request, final ResponseListenerInterface rli){

    final AsyncWebServiceResponseListener listener = new AsyncWebServiceResponseListener(ctx, request, rli);

    IntentFilter responseIntent = new IntentFilter(CommsActions.SEND);
    responseIntent.addCategory(request.getMessageType());
    ctx.registerReceiver(listener, responseIntent);

    try {
               ctx.startService(intent);
    } catch (Exception e) {
        Log.e(LOG_TAG , Log.getStackTraceString(e));
        );
    }
}

Appreciate any input.

Upvotes: 1

Views: 1612

Answers (1)

brybry
brybry

Reputation: 163

Long story short - I didn't realise that I was actually trying to mock an AMQP call, rookie move.

Luckily I was able to simplify the code by converting these to HTTP requests using retrofit as the application only really requires the sending of data to be performed over AMQP while retrieving can be a simple HTTP GET, which allowed for easy implementation of mockwebserver and actually sped up requests.

Upvotes: 0

Related Questions