I'm newbie writing unit tests for my Android app. Currently I'm testing ContentProviders (like here) and I recently finished one and all fine doing CRUD operations tests for a "satellite" SQLite table ("satellite"=no foreign keys). Now I have to test another ContentProvider that perform operations in other table that have a foreign key to "satellite" table AND filesystem (images) files.
My issues are (questions reference to the ContentProvider being tested):
- How to test ContentProvider if I need data from "satellite" table and (images) files? I need to create (in
setUp()
method) whole scenario (satellite data and images files) before running tests? And how? Think about this scenario:select * from table join satellite where satellite.id = ?
(query return a path to image column too). I mean, tables and file system have to have some data to ensure query return rows and test if file exists, right? ContentProvider is designed as an endpoint to trade data for SQlite table and images files in this way:
insert()
method receive temporal image file path and user data associated to the image. This ContentProvider move temporal image file to it's own logic inside custom path; and insert user data and previous path to SQlite table. Nextquery()
method return image path and user data as a row. While inserting, I have to call an utility static method that return custom path, for instance this method:Uri origPath( Context context, long idC, long idP, String fileName )
called usingFileUtility.origPath(...)
insideContentProvider#insert()
(Context param came from testing ContentProvider). When testing, I have ajava.lang.UnsupportedOperationException at android.test.mock.MockContext.getExternalFilesDir(MockContext.java:186)
I know that external/system calls by default throw an exception like that, but how to test my ContentProvider if it do a call to a static method from utility class to work? (like this) I need to refactor my code to adapt it to test work? or I have to change my test code for something else?
Can I call a ContentProvider from other ContentProvider? or is a bad design?
- Is more useful to create a FileProvider and deal with all user camera images with that provider?, By design it have to be called inside ContentProvider anyway (like 3.)
Feel free to explain how my design can change for sake of code lord, or if there is a pattern for that, or I missing something.