0

I have a CSV file that I want to make available for my Android testing suite. I don't want it to be available for the main app, though. I saved it in the assets folder of my test project. Then I try to access it from a test using

context.getAssets().open(DATA_ASSET);

where DATA_ASSET is declared as

DATA_ASSET = "cards.csv"

I set the context to refer to the Activity which I am testing. However, this obviously won't work as I have it since it will look in the assets of the main app. I see two possibilities to fix this:

  1. Provide the correct path to the asset installed with the test project
  2. Obtain a Context which refers to the test project's resources and assets

I haven't found any way to do either of these yet. Perhaps there is a third solution that I haven't thought of. How can I access the assets that are installed with the test project?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • It's not a clean fix if you're subclassing `AndroidTestCase`, but [this answer](http://stackoverflow.com/a/8870318/357055) might help you to get a `Context` for your test project. – acj Oct 30 '12 at 23:25
  • @acj Mostly I'm subclassing `ActivityInstrumentationTestCase2`, but some of my tests subclass `ActivityUnitTestCase` instead. – Code-Apprentice Oct 30 '12 at 23:29
  • possible duplicate of [Get context of test project in Android junit test case](http://stackoverflow.com/questions/8605611/get-context-of-test-project-in-android-junit-test-case) – Code-Apprentice Oct 30 '12 at 23:31
  • @acj That looks like the solution I need. Thanks! I'll try it out to see if it works. – Code-Apprentice Oct 30 '12 at 23:32
  • @acj Sweet! It seems to work. – Code-Apprentice Oct 30 '12 at 23:45

1 Answers1

1

If you're subclassing InstrumentationTestCase or a similar class, you can obtain a Context for the test project by doing the following:

Context ctx = getInstrumentation().getContext();
acj
  • 4,821
  • 4
  • 34
  • 49