Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-volley how to unit test passed in Listener

I have some requests I'm making with Android Volley. As the Listeners are doing things like turning the response JSON into objects, I'd like to test them to make sure they're doing the right thing. Thing is, I'm not very caught up on how to do unit testing. I do have Robolectric with JUnit set up, but any help would be appreciated. How would I go about setting up my test so I can test the Listener object passed into the request?

like image 823
s73v3r Avatar asked Sep 04 '13 07:09

s73v3r


1 Answers

It's enough to look at CacheDispatcher:

        Response<?> response = request.parseNetworkResponse(
                new NetworkResponse(entry.data, entry.responseHeaders));

This is where the request's response is created, using abstract parseNetworkResponse method (in case that you have implemented it), and then:

mDelivery.postResponse(request, response);

which actually fires the listeners, if you dig into the code. Rest of the stuff is thread related. I'd reccomend implementing simple testing routine that takes static NetworkResponse, and calls mDelivery's postResponse.

This actually also means, that you could possibly not go this way - it is enough to test which method (Response.success or Response.error) was called - this is your first unit test. Secondly, just test your listeners.

like image 144
Tomek Jurkiewicz Avatar answered Sep 19 '22 05:09

Tomek Jurkiewicz