Loading Resources from Unit Tests
There are a handful of reasons why you’d want to load in a resource file for a test:
- Input (or output) data is large and complex
- Input (or output) data is not easily mockable, such as a
- Configuration data is preferably externalized
One option would be to place your resources in your app’s
/assets directory. This requires instrumenting your tests so that you can access the resources through the app’s
Context. This can be undesirable for two reasons:
- If your tests do not require any additional instrumentation, then you’re sacrificing the speediness of running tests on a JVM.
- You’re testing against a library and therefore don’t have an app to instrument.
Accessing resources from plain-old JUnit tests turns out to be fairly straightforward. Place any resources into a new directory named
/src/test/resources and then access them through the test’s
This method may not work for older versions of Android Studio or the Android Gradle plugin. Instead you have to explicitly copy the resource files into the module’s build directory. This can be done by writing a task in the
You will need to execute the task before running your tests. Moreover, the file name passed to the
ClassLoader will need to be prefixed with the resources directory (e.g.
resources/data.json). Since you’re explicitly copying the files over in this case, the directory name is arbitrary.