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?
Android-volley how to unit test passed in Listener
Asked Answered
Find a way to mock the request manager so you can test the main request class by passing it static controlled content. I am in the exact same spot and will hopefully find a solution this week. –
Tarkany
If you are successful, would you please post here? Even though that project is over, I'd like to know for future reference. –
Wescott
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.
I forgot to state it, but there's no need to test that listener or errorListener were called in case of proper use of Response.success/error methods, according to unit testing principles. Unit tests of the volley module itself should do that work. –
Fanchette
© 2022 - 2024 — McMap. All rights reserved.