Unit testing with Android volley -


i know how can create unit tests volley framework. mock requests, responses can create unit tests wont require webservice working , network accesss.

i've googled don't find information framework @ all

i implemented subclass of httpstack named fakehttpstack load fake response body local file located in res/raw. did development purpose, i.e., can develop new api before server ready, may learn (e.g., overriding httpstack#peformrequest , createentity) here.

/**  * fake {@link httpstack} returns fake content using resource file in res/raw.  */ class fakehttpstack implements httpstack {     private static final string default_string_response = "hello";     private static final string default_json_response = " {\"a\":1,\"b\":2,\"c\":3}";     private static final string url_prefix = "http://example.com/";     private static final string logger_tag = "stack_over_flow";      private static final int simulated_delay_ms = 500;     private final context context;      fakehttpstack(context context) {         this.context = context;     }      @override     public httpresponse performrequest(request<?> request, map<string, string> stringstringmap)             throws ioexception, authfailureerror {         try {             thread.sleep(simulated_delay_ms);         } catch (interruptedexception e) {         }         httpresponse response                 = new basichttpresponse(new basicstatusline(httpversion.http_1_1, 200, "ok"));         list<header> headers = defaultheaders();         response.setheaders(headers.toarray(new header[0]));         response.setlocale(locale.japan);         response.setentity(createentity(request));         return response;     }      private list<header> defaultheaders() {         dateformat dateformat = new simpledateformat("eee, dd mmm yyyy hh:mm:ss zzz");         return lists.<header>newarraylist(                 new basicheader("date", dateformat.format(new date())),                 new basicheader("server",                         /* data below header info of server */                         "apache/1.3.42 (unix) mod_ssl/2.8.31 openssl/0.9.8e")         );     }      /**      * returns fake content using resource file in res/raw. fake_res_foo.txt used      * request http://example.com/foo      */     private httpentity createentity(request request) throws unsupportedencodingexception {         string resourcename = constructfakeresponsefilename(request);         int resourceid = context.getresources().getidentifier(                 resourcename, "raw", context.getapplicationcontext().getpackagename());         if (resourceid == 0) {             log.w(logger_tag, "no fake file named " + resourcename                     + " found. default fake response should used.");         } else {             inputstream stream = context.getresources().openrawresource(resourceid);             try {                 string string = charstreams.tostring(new inputstreamreader(stream, charsets.utf_8));                 return new stringentity(string);             } catch (ioexception e) {                 log.e(logger_tag, "error reading " + resourcename, e);             }         }          // return default value since no fake file exists given url.         if (request instanceof stringrequest) {             return new stringentity(default_string_response);         }         return new stringentity(default_json_response);     }      /**      * map request url fake file name      */     private string constructfakeresponsefilename(request request) {         string requrl = request.geturl();         string apiname = requrl.substring(url_prefix.length());         return "fake_res_" + apiname;     } } 

to use fakehttpstack, have pass requestqueue. override requestqueue too.

public class fakerequestqueue extends requestqueue {     public fakerequestqueue(context context) {         super(new nocache(), new basicnetwork(new fakehttpstack(context)));     } } 

good point approach that, doesn't require change in code. have switch requestqueue fakerequestqueue when testing. thus, can used in acceptance testing or system testing.

on other hand, unit testing, there might more compact way. instance, can implement request.listener subclass separate class onresponse method can tested. recommend put more detail want test or put code fragment.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -