Search code examples
androidunit-testingjunitassets

Mock access to /asset files


I'm trying to create a test unit for a class accessing a file in /asset and, as far as I have experimented, I see no way do that without altering the logic of the class being tested.

What I would like to accomplish is the creation of an environment where I can point my class to the asset file I like (and not the one originally pointed in the code), so that I can run multiple tests with it, so for example, if my original code looks something like:

ctx.getAssets().open("config.txt")

i can instead open

bogus_config.txt
ok_config.txt
nonexisting_config.txt

So far I have tried:

  • creating a MockContext, but since pretty much everything in AssetManager is final, I see no way to get the served the file I want
  • using a RenamingDelegatingContext with a specific prefix, but the prefix doesn't work on asset files it seems...

Is there a better way (some Java Magik Reflection Trick(Tm) of sorts, for instance) to achieve what I want?

Even though I'm very new to unit testing in general, I know that I could pick any of the following work-around solutions:

  • move the file into raw and use Resources (but what will I do when using assets will be mandatory? It would be nice to know...)
  • exposing the name of the file in the class and change it within the test unit (just kidding... ugh!! :p)
  • create a public wrap around of my method to use a specific file instead of the one used by default, and prefix the method with junit_ to document that it is to be used by testing routines only (which is probably what I'll end up doing)

but I still think that the original question might be of some interest, so any suggestion is welcome

Thank you!


Solution

  •   > I see no way do that without altering the logic of the class being tested.
    

    I agree but the change would be quite minimal: move the code ctx.getAssets().open("config.txt") into a protected method InputStream openConfig(Context ctx) which you can easily overide/fake/mock in your tests. If you use Eclipse or Androidstudio mark the statement and execute context-menu Refactor/Extract-Method.