A Retrofit Client that reads JSON files from disk and returns them as the response (to be handled by GSON). Make sure the context you pass is from the instrumentation app and put your JSON files in res/raw
in the instrumentation project. Inspired by Ruby's vcr
gem.
A GET
request to /articles
will look for the JSON file res/raw/get_articles.json
, a POST
request to /articles/123/
will look for the JSON file res/raw/post_articles_123.json
, etc.
We use setScenario()
if we want to test alternate responses so that a failed login POST
to /login
will read from res/raw/failed_login_post_login.json
(and the happy path can use post_login.json
).
@f2prateek sorry for the 3 month late reply - yes that is correct about other data formats. I actually use the above code as-is with an XML route in the same app.
#guessContentTypeFromStream()
does the magic of setting the right mimetype.No reason on
raw
vsassets
- just where I happened to put them :)